1 / 14

Character Strings

Character Strings. Character Strings. printf (&quot;Programming in C is fun.<br>&quot;); The argument that is passed to the printf function is the character string. &quot;Programming in C is fun.<br>&quot; The double quotation marks are used to delimit the character string.

donnan
Download Presentation

Character Strings

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. Character Strings

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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!”;

  7. 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.

  8. 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.”;

  9. 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”;

  10. 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]);

  11. null character ‘\0’ == 0 • The null character (‘\0’), which is always evaluated as a value of zero.

  12. 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; }

  13. Initializing Strings • Word [] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };

More Related