LECTURE 19: Strings

 

String Basics

 

In C, a string is implemented as an array of characters, and the null character '\0' that is added at the end of every string. The characters after null are not processed by a program.

 

For example, the declaration

char str[20] = "Initial value";

will have the following memory content associated to variable name str:

 

[0]         [4]           [9]         [13]              [19]

 I  n  i  t  i  a  l    v  a  l  u  e  \0

 

Consequently, the size of an array should be at least one more than the length of the string it contains.

An array of strings is a two-dimensional array of characters. For example,

char month[12][10] = {  "January", "February", "March", April", "May", "June",

                  "July", "August", "September", "October", "November",

                  "December"};

 

Note: month[6] is the address of the 7th string of month, which is the string “July”.

 

Strings in printf and scanf

 

A placeholder for string in printf and scanf is written as "%s".

 

Examples:

printf(“%s\n”,str) // prints all characters of str until ‘\0’

printf(“%s \n”,month[6]);  // prints the sevents string of array month (“July”)

 

Note, is a string does not contain ‘\0’ a runtime error will often occur

Since an array parameter is passed by reference, the "&" operator should not be used. The function skips leading whitespace characters, starts at the first non-whitespace character, copies it into the character array, and continues copying the following characters, until a whitespace character is encountered. Finally, a null character is put at the end of the array.

 

Examples:

scanf(“%s”,str); // reads a string and copies it to str

scanf(“%s”,month[0]);  // reads a string and copies it to the first string of month array

 

String Initialization

 

For stings, the assignment operator "=" can only be used in initialization, such as

char con_str[20] = "Testing string";  /* works fine */

but cannot be used later to change the value, such as

char one_str[20];

one_str = "Testing string";           /* does not work */

This is the case, because the string name indicates a constant address that cannot be changed using assignment.

 

String Library Functions

 

Instead of using the assignment operator, string manipulation in C is carried out by various library functions defined in string.h. Among the functions, stpcpy copies one string into another, and strncpy copies the first n characters from one string into another. Both functions are restricted by the size of the destination string. Copy can be made only when there is space in the destination. Also, the ending null character may be lost.

 

Declaration of function strncpy:

char *strncpy(char *dest, const char *src, size_t len);

So, function arguments are pointers to destination and source strings, as well as the number of characters to be copied. The function returns the pointer to dest string if its outcome was successful, and NULL pointer in case of failure.

 

Example:

char result[20]; // after this line content of result are 20 junk values

char s1[20] = “Jan. 30, 1996\0”;

strncpy(result,s1,9); // content of result is “Jan. 30, “

Memory state at the end of function call

strncpy(result,s1,9);

can be visualized as (FIGURE 9.5):

To call the functions with a subscript will lead to an operation at a substring, because a string name indicates nothing but the starting address of the array to be processed.

Memory state at the end of function call

strncpy(result, &s1[5], 2); // &s1[5] is address of the sixth character of source string

can be visualized as (FIGURE 9.6):

 

Longer strings

 

Library functions strcat and strncat modify their first argument by adding all or part of their second argument at the end of the first. This operation is called "concatenation", and it assumes that in the destination there is enough space to hold the added characters. Please note that the null character at the end of the destination is removed before new characters are added.

 

Declaration of function strcat:

char * strcat(char * dest, const char *src);

 

The functions defined on strings cannot take a char as argument. If you wish to add a single character at the end of a string, you should view the string as an array, and use assignment to subscripted elements for access. Be sure to include the null character at the end of the string.

 

Another useful function is strlen. Declaration of function strlen:

size_t strcat(const char *src);

It returns length of the src string.

 

Arrays of pointers

 

We can define an array of pointer to store a list of strings. For example,

 

char days[7][10] = {“Monday”, ”Tuesday”, ”Wednesday”, ”Thursday”, ”Friday”, ”Saturday”, ”Sunday”};

or

char *days[7] = {“Monday”, ”Tuesday”, ”Wednesday”, ”Thursday”, ”Friday”, ”Saturday”, ”Sunday”};