140 likes | 315 Views
Character Strings. Character Strings. printf ("Programming in C is fun.<br>"); The argument that is passed to the printf function is the character string. "Programming in C is fun.<br>" The double quotation marks are used to delimit the character string.
E N D
Character Strings • printf ("Programming in C is fun.\n"); • The argument that is passed to the printf function is the character string. "Programming in C is fun.\n" • The double quotation marks are used to delimit the character string. • The C compiler will automatically add a null character (\0) at the end of a string constant to indicate the end of the string.
Arrays of Characters • A string is a character array, with a null character (\0) used to mark the end of the string. char array_ch[7] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’}; • In C, the null character is used to mark the end of a string, and it always evaluates to 0. C treats \0 as one character. • Each character in a string takes only 1 byte.
character array character array can be declared and initialized like this: • char arr_str[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’}; • Here the array arr_stris treated as a character array.
String array • If you add a null character (\0) into the array, you can have the following statement: • char arr_str[7] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’}; • Here the array arr_str is expanded to hold seven elements; the last element contains a null character. Now, the character array arr_str is considered a character string because of the null character that is appended (added to the end) of the character data.
Initializing Strings • You can also initialize a character array with a string constant, instead of a list of character • constants. • For example, the following statement initializes a character array, str, with a string constant, “Hello!”: char str[7] = “Hello!”;
Initializing Strings… • The compiler will automatically append a null character (\0) to the end of “Hello!”, and treat the character array as a character string. Note that the size of the array is specified to hold up to seven elements, althoughthe string constant has only six characters enclosed in double quotes.
Initializing unsized character array • You can declare an unsized character array if you want the compiler to calculate the total • number of elements in the array. For instance, the following statement: • char str[] = “I like C.”;
Don’t specify the size of a character array as too small • The following declaration is considered illegal: char str[4] = “text”; • Note that many C compilers will not issue a warning or an error message on this incorrect declaration. • Therefore, it’s your responsibility to • make sure to specify enough space hold the string constant plus an extra null character. • char str[5] = “text”;
Printing an Array of Characters • char array_ch[7] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ‘\0’}; //first method inti; for (i=0; i<7; i++) printf(“array_ch[%d] contains: %c\n”, i, array_ch[i]); //second method for (i=0; array_ch[i] != ‘\0’ && i<7; i++) printf(“%c”, array_ch[i]);
null character ‘\0’ == 0 • The null character (‘\0’), which is always evaluated as a value of zero.
Strings Example #include <stdio.h> main() { char str1[] = {'A', ' ','s', 't', 'r', 'i', 'n', 'g', ' ', 'c', 'o', 'n', 's', 't', 'a', 'n', 't', '\0'}; char str2[] = "Another string constant"; char *ptr_str; inti; /* print out str1 */ for (i=0; str1[i]; i++){ printf("%c", str1[i]); } printf("\n"); /* print out str2 */ for (i=0; str2[i]; i++){ printf("%c", str2[i]); } printf("\n"); /* assign a string to a pointer */ ptr_str = "Assign a string to a pointer."; for (i=0; *ptr_str; i++){ // printf("%c", *ptr_str++); printf("%c", (*ptr_str)); ptr_str++; } system("pause"); return 0; }
Initializing Strings • Word [] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };