C43. C Strings all topics covered


String Literals
A string literal is a sequence of characters enclosed within double quotes:
string literals  often appear as format strings in calls of print f and scanf.
Escape Sequences in String Literals
String literals may contain the same escape sequences as character constants. We've used character escapes in printf and scanf format strings for some time. For example, we've seen that each \n character in the string
"Candy\nls dandy\nBut liquor\nIs quicker.\n   Ogden Nash\n"
causes the cursor to advance to the next line:
Is dandy
 But liquor
Is quicker.
 Ogden Nash
Although octal and hexadecimal escapes are also legal in string literals, they're not as common as character escapes.

How String Literals Are Stored
We've used string literals often in calls of printf and scanf. But when we call printf and supply a string literal as an argument, what are we actually passing? To answer this question, we need to know how string literals are stored.
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 bytes 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 a byte whose bits are all zero, so it's represented by the \0 escape sequence.

String Variables
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. This approach is simple, but has significant difficulties. It's sometimes hard to tell whether an array of characters is being used as a string. If we write our own string-handling functions, we've got to be careful that they deal properly with the null character. Also, there's no faster way to determine the length of a string than a character-by-character search for the null character.
Let's say that we need a variable capable of storing a string of up to 80 charac­ters. Since the string will need a null character at the end, we'll declare the variable to be an array of 81 characters:
#define STR_LEN 80
char str[STR_LEN+1];
We defined STR_LEN to be 80 rather than 81, thus emphasizing the fact that str can store strings of no more than 80 characters, and then added 1 to STR_LEN in the declaration of str. This a common practice among C programmers.
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 of the C convention that every string is terminated by a null character. Failing to leave room for the null character may cause unpredictable results when the program is executed, since functions in the C library assume that strings are null-terminated.

Reading and Writing Strings
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.
Writing Strings Using printf and puts
The %s conversion specification allows printf to write a string. printf writes the characters in a string one by one until it encounters a null char­acter. (If the null character is missing, printf continues past the end of the string until—eventually—it finds a null character somewhere in memory.).
printf isn't the only function that can write strings. The C library also pro­vides puts, which is used in the following way:
puts has only one argument (the string to be printed). After writing the string, puts always writes an additional new-line character, thus advancing to the begin­ning of the next output line.

Reading Strings Using scanf and gets
The %s conversion specification allows scanf to read a string into a character array:
scanf("%s", str);
There's no need to put the & operator in front of str in the call of scanf; like any array name, str is treated as a pointer when passed to a function. 1.2 When scanf is called, it skips white space, then reads characters and stores them in 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, scanf won'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 doesn't skip white space before starting to read the string (scanf does).

gets reads until it finds a new-line character (scanf stops at any white- space character). Incidentally, gets discards the new-line character instead of storing it in the array; the null character takes its place.

The strcpy (String Copy) Function
The strcpy function has the following prototype in : char*strcpy(char *sl, const char *s2);
strcpy copies the string s2 into the string s1. (To be precise, we should say "strcpy copies the string pointed to by s2 into the array pointed to by s1.") That is, strcpy copies characters from s2 to s1 up to (and including) the first null character in s2. strcpy returns s1 (a pointer to the destination string). The string pointed to by s2 isn't modified, so it's declared const.The existence of strcpy compensates for the fact that we can't use the assignment operator to copy strings.

Calling the strncpy function is a safer, albeit slower, way to copy a siring, strncpy is similar to strcpy but has a third argument that limits the number of characters that will be copied. To copy str2 into strl, we could use the follow­ing call of strncpy.

strncpy(strl, str2, sizeof(strl));
As long as strl is large enough to hold the string stored in str2 (including the null character), the copy will be done correctly, strncpy itself isn't without dan­ger, though. For one thing, it will leave the string in strl without a terminating null character if the length of the string stored in str2 is greater than or equal to the size of the strl array. Here's a safer way to use strncpy:
strncpy(strl, str2, sizeof(strl) - 1);
strl [sizeof(strl)-1] = '\0';
The second statement guarantees that strl is always null-terminated, even if strncpy fails to copy a null character from str2.
The strlen (String Length) Function
The strlen function has the following prototype:
Size_t strlen(const char *s);

Size_t. which is defined in the C library, is a typedef name that represents one of C's unsigned integer types. Unless we're dealing with extremely long strings, this technicality need not concern us—we can simply treat the return value of strlen as an integer.
strlen returns the length of a string s: the number of characters in s up to, but not including, the first null character.

The strcat (String Concatenation) Function
The strcat function has the following prototype:
char *strcat (char *sl, const char *s2)
strcat appends the contents of the string s2 to the end of the string s1;
it returns s1 (a pointer to the resulting string).
Here are some examples of strcat in action:
strcpy(strl, "abc");
strcat(strl, "def");  /* strl now contains "abedef" */

The strncat function is a safer but slower version of strcat. Like strncpy, it has a third argument that limits the number of characters it will copy. Here's what a call might look like:
strncat(strl, str2, sizeof(strl) - strlen(strl) - 1) ;
strncat will terminate strl with a null character, which isn't included in the third argument (the number of characters to be copied). In the example, the third argument calculates the amount of space remaining in strl (given by the expres­sion sizeof (strl) - strlen (strl)) and then subtracts 1 to ensure that there will be room for the null character.
The strcmp (String Comparison) 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.

No comments: