1 / 31

A string literal is a sequence of characters enclosed within double quotes:

Chapter 13. This chapter covers both string constants (or literals , as they're called in the C standard) and string variables , which can change during the execution of a program. A string literal is a sequence of characters enclosed within double quotes:

wynn
Download Presentation

A string literal is a sequence of characters enclosed within double quotes:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution of a program. A string literal is a sequence of characters enclosed within double quotes: "Put a disk in drive A, then press any key to continue\n“ We first encountered string literals in Chapter 2; they often appear as format strings in calls of printf and scanf.

  2. For example, we've seen that each \n character in the string "Candy\nis dandy\nBut liquor\nis quicker.\n --Ogden Nash\n“\ causes the cursor to advance to the next line: Candy is dandy But liquor is quicker. --Ogden Nash Although octal and hexadecimalescapes are also legal in string literals, they're not as common as character escapes.

  3. If we find that a string literal is too long to fit conveniently on a single line, C allows us to continue it on the next line, provided that we end the first line with a \ character. No other characters may follow \ on the same line, other than the (invisible) new-line character at the end: printf("Put a disk in drive A, then \ press any key to continue\n,"); The \ technique has one drawback: the string must continue at the beginning of the next line, thereby wrecking the program's indented structure. A better way to deal with long string literals is when two or more string literals are adjacent (separated only by white space), the compiler must join them into a single string. This rule allows us to split a string literal over two or more lines: printf("Put a disk in drive A, then " "press any key to continue\n") ;

  4. In essence, C treats string literals as character arrays. When a C compiler encounters a string literal of length n in a program, it sets aside n + 1 characters of memory for the string. This area of memory will contain the characters in the string, plus one extra character, the null character, to mark the end of the string. The null character is the very first character in the ASCII character set, so it's represented by the \ 0 escape sequence.

  5. For example, the string literal " abc" is stored as an array of four characters (a, b, c, and \0): Since a string literal is stored as an array, the compiler treats it as a pointer of type char *. Both printf and scanf, for example, expect a value of type char * as their first argument.

  6. In general, we can use a string literal wherever C allows a char * pointer. For example, a string literal can appear on the right side of an assignment: char *p ; p = "abc" ; This assignment does not copy the characters in " abc"; It makes p point to the first character of the string. C allows pointers to be subscripted, so we can subscript string literals: char ch; ch = " abc" [ 1 ] ; The new value of ch will be the letter b. The other possible subscripts are 0 (which would select the letter a), 2 (the letter c), and 3 (the null character).

  7. A string literal containing a single character is not the same as a character constant. The string literal " a" is represented by a pointer to a memory location that contains the character a (followed by a null character). The character constant 'a' is represented by an integer (the ASCII code for the character). See the ASCII Character Set in Appendix E of your textbook (p.637).

  8. Some programming languages provide a special string type for declaring string variables. C takes a different tack: any one-dimensional array of characters can be used to store a string, with the understanding that the string is terminated by a null character. When declaring an array of characters that will be used to hold a string, always make the array one character longer than the string, because the C convention that every string is terminated by a null character.

  9. A string variable can be initialized at the point of declaration: char date1[8] = "June 14"; The compiler will copy the characters from "June 14" into the date1 array, then add a null character so that date1 can be used as a string.

  10. When an array initializer is shorter than the array itself, the remaining elements are initialized to 0. char date2[9] = "June 14";

  11. If you're planning to initialize a character array to contain a string, be sure that the length of the array is longer than the length of the initializer. Otherwise, the compiler will quietly omit the null character, making the array unusable as a string. char date3[7] = "June 14";

  12. The declaration of a string variable may omit its length, in which case the compiler computes it: char date4[ ]= "June 14"; The compiler sets aside eight characters for date4, enough to store the characters in "June 14" plus a null character.

  13. Let's compare the declaration char date[] = "June 14"; which declares date to be an array of characters, with the similar-looking char *date = "June 14"; which declares date to be a pointer to a string literal. There are significant differences between the two: In the array version, the characters stored in date can be modified, like the elements of any array. In the pointer version, date points to a string literal, and as discussed in Section 13.1 string literals should not be modified. In the array version, date is an array name. In the pointer version, date is a variable that can be made to point to other strings during program execution.

  14. Writing a string is easy using either the printf or puts functions. Reading a string is a bit harder, primarily because of the possibility that the input string may be longer than the string variable into which it's being stored. To read a string in a single step, we can use either scanf or gets. As an alternative, we can read strings one character at a time. The %s conversion specification allows printf to write a string. Consider the following example: char str[ ] = "Are we having fun yet?"; printf("Value of str: %s\n", str); The output will be Value of str: Are we having fun yet?

  15. To print just part of a string, we can use the conversion specification % .ps, where p is the number of characters to be displayed. The statement printf("%.6s\n", str); will print Are we The m and p values can be used in combination: a conversion specification of the form %m.ps causes the first p characters of a string to be displayed in a field of size m. printf isn't the only function that can write strings. The C library also provides puts, which is used in the following way: puts(str) ; puts has only one argument, the string to be printed; there is no format string. After writing a string, puts always writes an additional new-line character, thus advancing to the beginning of the next output line.

  16. The %s conversion specification allows scanf to read a string: scanf("%s", str) ; When scanf is called, it skips white space, then reads characters and stores them into str until it encounters a white-space character, scanf always stores a null character at the end of the string. A string read using scanf will never contain white space. Consequently, scanfwon't usually read a full line of input; a new-line character will cause scanf to stop reading, but so will a space or tab character. To read an entire line of input at a time, we can use gets. Like scanf, the gets function reads input characters into an array, then stores a null character. In other respects, however, gets is somewhat different from scanf: gets does notskip white space before starting to read the string, scanf does getsreads until new-line character, scanfstops at white-space character.

  17. char sentence[SENT_LEN+1]; printf("Enter a sentence:\n"); scanf("%s", sentences); Enter a sentence: the user enters the line To C, or not to C: that is the question. scanf will store the string "To" into sentence. The next call of scanf will resume reading the line at the space after the word To..Now suppose that we replace scanf by gets: gets(sentence) ; When the.user enters the same input as before, gets will store the string " To C, or not to C: that is the question." into sentence.

  18. Since strings are stored as arrays, we can use subscripting to access the characters in a string. To process every character in a string s, for example, we can set up a loop that increments a counter i and selects characters via the expression s [ i ]. Suppose that we need a function that counts the number of spaces in a string. Using array subscripting, we might write the function in the following way:

  19. Let's rewrite the count_spaces function using pointer arithmetic instead of array subscripting. We'll eliminate the variable i and use s itself to keep track of our position in the string. By incrementing s repeatedly, count_spaces can step through each character in the string. Note that const doesn't prevent count_spaces from modifying s; it's there to prevent the function from modifying what s points to.

  20. • Is it better to use array operations or pointer operations to access the characters in a string? Traditionally, C programmers use pointer operations for processing strings. • Should a string parameter be declared as an array or as a pointer? The two versions of count_spaces illustrate the options: the first version of count_spaces declares s to be an array; the second version declares s to be a pointer. Actually, there's no difference between the two. • Does the form of the parameter (s [ ] or *s) affect what can be supplied as an argument? No. When count_spaces is called, the argument could be an array name, a pointer variable, or a string literal, count_spaces can not tell the difference.

  21. The C library provides a set of functions for performing operations on strings. Prototypes for these functions reside in the <string.h> so programs that need string operations should contain the following line: #include <string.h> The strcpy (String Copy) Function The strcpy function has the following prototype in <string. h>: char *strcpy(char *s1, const char *s2); strcpy copies the string s2 into the string s1. The strcat (String Concatenate) Function The strcat function has the following prototype: char *strcat(char *s1, const char *s2); strcatappends the contents of the string s2 to the end of the string s1; it returns s1 (a pointer to the resulting string).

  22. The strcmp (String Compare) Function The strcmp function has the following prototype: int strcmp(const char *s1, const char *s2); strcmp compares the strings s1 and s2, returning a value less than, equal to, or greater than 0, depending on whether s1 is less than, equal to, or greater than s2. The strlen (String Length) Function The strlen function has the following prototype: size_t strlen(eonst char *s) ; size_t, is defined in the C library, is an unsigned integer type (usually unsigned int or unsigned long int), we treat the return value of strlen as an integer. strlen returns the length of a string s.

  23. Searching for the End of a String Many string operations require searching for the end of a string. The strlen function is a prime example. The following version of strlen searches its string argument to find the end, using a variable to keep track of the string's length: size_t strlen(const char *s) { size_t n; for (n = 0; *s != '\0'; s++) n++; return n; } As the pointer s moves across the string from left to right, the variable n keeps track of how many characters have been seen so far. When s finally points to a null character, n contains the length of the string.

  24. Copying a string is another common operation. To introduce C's "string copy“ idiom, we'll develop a version of the strcat function. Let's start with a straight-forward but somewhat lengthy version of strcat: This version of s treat uses a two-step algorithm: (1) Locate the null character at the end of the string s1 and make p point to it. (2) Copy characters one by one from s2 to where p is pointing.

  25. The first while statement in the function implements step (1). p is set to point to the first character in s1. Assuming that s1 points to the string " abc", we have the following picture:

  26. p is then incremented as long as it doesn't point to a null character. When the loop terminates, p must be pointing to the null character:

  27. The second while statement implements step (2). The loop body copies one character from where s2 points to where p points, then increments both p and s2. If s2 originally points to the string " def", here's what the strings will look like after the first loop iteration:

  28. The loop terminates when s2 points to the null character: After putting a null character where p is pointing, s treat returns.

  29. Arrays of Strings What's the best way to store an array of strings? The obvious solution is to create a two-dimensional array of characters, then store the strings in the array, one per row. Consider the following example: char planets[ ][8] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto") ;

  30. Here's the planets array again, this time as an array of pointers to strings: char *planets[ ] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}; Each element of planets is a pointer to a null-terminated string. There are no longer any wasted characters in the strings, although we've had to allocate space for the pointers in the planets array.

More Related