Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.

Similar presentations


Presentation on theme: "Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples."— Presentation transcript:

1 Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples of character string constant are as follows. “C for Engineers” “Programming is fun” C f o r E n g i n e e r s \0 Null character

2 9.2 Declaring and Initializing Strings A character string in C is an array of type char. As with arrays of other data types, all character strings have to be declared. In addition to the name of the string, we have to tell the compiler how many characters there will be in the string so that the compiler can provide the necessary storage to hold the characters. There are two ways in which we can declare a character string: 1. Array notation 2. Pointer notation 9.2.1 The Array Form When we declare a character string using array notation, we have to tell the compiler how many characters there will be in the string and also the name of the array.

3 char name [20], buffer [11]; This statement creates two arrays of type char. The array name[] consists of 20 elements, while the array buffer has 11 elements. Thus the array name[] can store a string of 19 characters, while the array buffer can store a string of 10 characters. | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Buffer [ ] buffer

4 9.2.2 The Pointer Form Character string can also be declared using pointers as in the statement. Char *planet = “jupiter”; The C compiler stores this as a string of eight characters. In addition, the C compiler will also creates a pointer to type char called planet. The pointer planet initially pointers to the first character of the string “jupiter”, that is, it contains the address in memory of the character ‘j’. However, planet is a variable and can be changed in the program. For example, we can use the increment operator as in ++ planet to change its value. Now, planet points to the second character in the string “jupiter”.

5 | j | u | p | i | t | e | r | \0 | 0 1 2 3 4 5 6 7 planet planet++ | j | u | p | i | t | e | r| \0 | 0 1 2 3 4 5 6 7 planet

6 9.3 String Input and Output Two steps are in reading a string in to a program. First, we must create a character array and set aside sufficient space to store the string. Second, we must use a string input function to read in the string. We create space for the string by including and explicit size in the array declaration. For example, the statement. Char buffer [81]; creates a character array buffer[ ] consisting of 81 elements. We can use the scanf() function with the %s format specifier to read in a string as in scanf(“%s”, buffer);

7 There is no address-of (&) operator preceding the name of the array. This is because the name of the array is also the address of the first element of the array. 9.3.1 The gets() Function The gets() library function (for get string) reads in a line of text from the standard input device. The following example reads in a string and prints a greeting. #include void main(void) { char name[81]; printf(“\n What is your name? “); gets(name); printf(“\n Greetings %s”, name); }

8 9.3.2 The puts () Function The puts() function (for put string) prints a string on the screen. It automatically adds a newline character to the end of the string. For example, the statement puts(“Greetings”); 9.4 Determining The Length of Strings The C library strlen() function returns the length of a string. The argument to the function is the address of a null terminated string. The function counts the number of characters in the string. The null character is not included in the count. We can also determine the number of characters in a string using the sizeof() operator. The sizeof operator includes the null character in its count.

9 Example: The strlen() Function The program reads several character strings and prints the string and the length of the string. It uses the strlen() function to determine the length of each string. #include void mani(void) { char buffer [ ] = “First String”; printf(“\n The length of the string is: %d”, strlen(buffer); } Program output The length of the string is: 12

10 9.5 Copying Strings The C library strcpy() function copies one string over another. It takes two arguments of type pointer to char. The two arguments are pointers that point to null terminated strings. The first string is the target string, and the second string is the source string for the copy operation. The function copies all characters in the source string (including the terminating null character) on to the target string. Thus the target string is overwritten, but the source string remains unchanged. The source string can be a string constant.

11 #include void main(void) { char target_str[50] = {“The first string”}; char *source_str = “Second string”; printf(“Before strcpy(): \n”); printf(“Target string = |%s|\n”, target_str); printf(“Source string = |%s|\n”, source_str); strcpy ( target_str, source_str ); printf(“After strcpy(): \n); printf(“Target string = |%s|\n”, target_str); printf(“Source string = |%s|\n”, source_str); }

12 Program output: Before strcpy(): Target string = |The first string| Source string = |Second string| After strcpy(); Target string = |Second string| Source string = |Second string|

13 9.6 Concatenating Strings The strcat() function adds or concatenates two strings str1 and str2 to form a single string. The result is stored in str1. The function removes the null character at the end of the first string and adds new null character at the end of the concatenated string. Thus str1 is changed, but str2 remains unchanged. Example: String Concatenation problem statement: write a program that combines two strings to form a single string.

14 #include void main(void) { char str1[11] = “12345”; char *str2 = “67890”; printf(“Before strcat(): \n”); printf(“Target string = |%s|\n”, str1); printf(“Source string = |%s|\n”, str2); strcat (str1, str2 ); printf(“After strcat(): \n); printf(“Target string = |%s|\n”, str1); printf(“Source string = |%s|\n”, str2); }

15 Program output: Before strcat(): Target string = |12345| Source string = |67890| After strcat(); Target string = |1234567890| Source string = |67890|

16 9.7 Comparing Strings Since strings are represented by character array in C, we cannot compare two strings using the relational operators such as ==, !=, and >=. Thus, if str1 and str2 are two strings, then we cannot use the following statement. If (str1 == str2)/* Wrong!!*/ to determine if the two strings are equal. The C library provides a function called strcmp() for comparing the contents of two strings. The strcmp() function accepts two pointers as arguments and returns the following values: 0if the two string are equal > 0if the first string is larger < 0if the first string is smaller

17 Any Questions


Download ppt "Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples."

Similar presentations


Ads by Google