Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.

Similar presentations


Presentation on theme: "Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology."— Presentation transcript:

1 Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 String a grouping of characters use arrays of type char

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-3 String Basics #define ERR_PREFIX "*****Error - " #define INSUFF_DATA "Insufficient Data" char string_var[30]; initialization of string variables using either a brace- enclosed character list, or a string constant –char str[20] = "Initial value"; null character –character '\0' that marks the end of a string in C

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-4 Arrays of Strings an array of strings is a two-dimensional array of characters in which each row is one string. –#define NUM_PEOPLE 30 –#define NAME_LEN 25 –... –char names[NUM_PEOPLE][NAME_LEN]; Initialization –char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-5 Input/Output with printf and scanf right-justified –printf ("***%8s***%3s***\n", "Short", "Strings"); left justified –printf("%-20s\n", president);

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-6 Input/Output with printf and scanf (Cont’d) Do not apply the address-of operator to a string argument passed to scanf The approach scanf takes to string input is very similar to its processing of numeric input. –scanf skips leading whitespace characters such as blanks, newlines, and tabs

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-7

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-8 Input/Output with printf and scanf (Cont’d) When it comes across a whitespace character, scanning stops, and scanf places the null character at the end of the string in its array argument.

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-9 Input/Output with printf and scanf (Cont’d) scanf would have difficulty if some essential whitespace between values were omitted or if a nonwhitespace separator were substituted. For Fig. 9.2: –MATH 1270 TR 1800 Even more… out of space problem! –MATH,1270,TR,1800

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-10 Example int ages[NUM_PEOPLE];

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-11 String Library Functions: Assignment and Substrings A declaration of a string variable with initialization is the only place in which the operator means to copy the string. An array name with no subscript represents the address of the initial array element, which is constant and cannot be changed through assignment. Invalid target of assignment: –char one_str[20]; –one_str = "Test string"; /* Does not work */

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-12

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-13 String Assignment char one_str[20] strcpy(one_str, "Test String"); Overflow: –strcpy(one_str, "A very long test string");

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-14 Limited size copying: strncpy strncpy(one_str, "Test string", 20); If the string to be copied (the source string) is shorter than n characters, the remaining characters stored are null.

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-15 Limited size copying: strncpy when the source string is longer than n characters, only the first n characters are copied. strncpy(one_str, "A very long test string", 20); no terminating '\0‘ ! Solution –strncpy(dest, source, dest_len - 1); –dest[dest_len - 1] = '\0';

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-16 Substrings Substring a fragment of a longer string Example –char result[10], s1[15] = "Jan. 30, 1996"; –strncpy(result, s1, 9); –result[9] = '\0'; –strncpy(result, &s1[5], 2); –result[2] = '\0';

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-17 Substrings (Cont’d)

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-18 Substrings (Cont’d)

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-19 Substrings (Cont’d)

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-20 Substrings (Cont’d) string length –in a character array, the number of characters before the first null character –strlen empty string –a string of length zero: the first character of the string is the null character

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-21 Concatenation –joining of two strings strcat, strncat Example #define STRSIZ 15 char f1[STRSIZ] = "John ", f2[STRSIZ] = "Jacqueline ", last[STRSIZ] = "Kennedy"; strcat(f1, last); strcat(f2, last); /* invalid overflow of f2 */ strncat(f2, last, 3);

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-22 Concatenation (Cont’d) Example of overflow prevention if (strlen(s1) + strlen(s2) < STRSIZ) { strcat(s1, s2); } else { strncat(s1, s2, STRSIZ - strlen(s1) - 1); s1[STRSIZ - 1] = '\0'; } Always be in the mind –Is there enough space in the output parameter? –Does the created string end in '\0'?

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-23 Characters and Strings character 'Q' and the string "Q"

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-24 Scanning a Full Line gets –input of one complete line of data –Example char line[80]; printf("Type in a line of data.\n"> "); gets(line); Input: > Here is a short sentence \n is not stored

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-25 Scanning a Full Line (Cont’d) fgets takes three arguments — the output parameter string, a maximum number of characters to store (n), and the file pointer to the data source. fgets will never store more than n -1 characters from the data file. The final character stored will always be '\0'. If fgets has room to store the entire line of data, it will include '\n' before '\0'. –If the line is truncated, no '\n' is stored. When a call to fgets encounters the end of file, the value returned is the address 0, which is considered the null pointer.

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-26

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-27

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-28 String Comparison str1 < str2 is not checking whether str1 precedes str2 alphabetically. function strcmp If the first n characters of str1 and str2 match and str1[n], str2[n] are the first nonmatching corresponding characters, str1 is less than str2 if str1[n] < str2[n].

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-29 String Comparison (Cont’d) If str1 is shorter than str2 and all the characters of str1 match the corresponding characters of str2, str1 is less than str2. –Str1: j o y –Str2: j o y o u s

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-30 String Comparison (Cont’d)

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-31 Arrays of Pointers

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-32 Arrays of Pointers (Cont’d) char *alphap[5]; Let –alphap[0] address of "daisy" –alphap[1] address of "marigold" –alphap[2] address of "petunia" –alphap[3] address of "rose" –alphap[4] address of "tulip" for (i = 0; i < 5; ++i) printf("%s\n", alphap[i]);

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-33 Arrays of Pointers (Cont’d)

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-34 Figure 9.14 Two Orderings of One List Using an Array of Pointers

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-35 Figure 9.14 Two Orderings of One List Using an Array of Pointers (cont’d)

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-36 Figure 9.14 Two Orderings of One List Using an Array of Pointers (cont’d)

37 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-37 Arrays of Pointers (Cont’d) several benefits –A pointer requires less storage space than a full copy of a character string. –The sorting function executes faster when it copies only pointers and not complete arrays of characters.

38 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-38 Arrays of String Constants Compare: –char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; –char *month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

39 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-39 Character Operations input –scanf("%c", &ch) –ch = getchar() –ch = getc(infilep) output –putchar('a'); –putc('a', outp);

40 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-40 Figure 9.15 Implementation of scanline Function Using getchar

41 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-41 Character Analysis and Conversion

42 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-42 Figure 9.16 String Function for a Greater- Than Operator That Ignores Case

43 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-43 Figure 9.16 String Function for a Greater- Than Operator That Ignores Case (cont’d)

44 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-44 String-to-Number and Number-to-String Conversions sprintf sscanf

45 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-45 String-to-Number and Number-to-String Conversions (Cont’d)

46 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-46 String-to-Number and Number-to-String Conversions (Cont’d)

47 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-47 Figure 9.18 Functions That Convert Representations of Dates

48 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-48 Figure 9.18 Functions That Convert Representations of Dates (cont’d)

49 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-49 Figure 9.18 Functions That Convert Representations of Dates (cont’d)

50 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-50 Figure 9.18 Functions That Convert Representations of Dates (cont’d)

51 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-51 Common Programming Errors Returning a string value in a function returns the address of the string in a local variable. –The problem with this approach is that the function’s data area is deallocated as soon as the return statement is executed, so it is not valid to access from the calling module the string the function constructed in its own local variable.

52 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-52 Figure 9.22 Flawed scanline Returns Address of Deallocated Space

53 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-53 Common Programming Errors (Cont’d) Misuse or neglect of the & operator to strings or to any other whole arrays Overflow of character arrays allocated for strings –Requiring an input argument telling how much space is available for the string result prevent storage of a string that is too long. Strings must end with the null character. Do not use equality or relational operators when comparing strings or the assignment operator for copying them. –Use strcmp or strncmp for comparisons –Use strcpy or strncpy for copying strings.

54 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-54 Figure 9.19 Structure Chart for Text Editor Program

55 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-55 Figure 9.20 Text Editor Program

56 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-56 Figure 9.20 Text Editor Program (cont’d)

57 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-57

58 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-58 Figure 9.20 Text Editor Program (cont’d)

59 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-59 Figure 9.20 Text Editor Program (cont’d)

60 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-60 Figure 9.20 Text Editor Program (cont’d)

61 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-61 Figure 9.21 Sample Run of Text Editor Program

62 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-62 Figure 9.23 Bands Encoding the Resistance Value of a Resistor


Download ppt "Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology."

Similar presentations


Ads by Google