String processing summary, string Summary
1. String input 1. Enter a single string
AvailableScanfFunction. The input string is separated by spaces. The Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str [100]; 6 // one input string, ending with a space of 7 while (scanf ("% s", str )! = EOF) {8 printf ("% s \ n", str); 9} 10 11 return 0; 12}
2. Enter the entire line of string
AvailableGetsFunction, which is separated by '\ n' and enters the entire line of string. The Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str [100]; 6 // use '\ n' as the separator and enter the entire line of string 7 while (gets (str )! = NULL) {8 printf ("% s \ n", str); 9} 10 11 return 0; 12}
Ii. string processing 1. String copying
AvailableStrcpyThe Code is as follows:
# Include <stdio. h> # include <string. h> int main () {char str1 [100], str2 [100]; // one input string, ending with a space while (scanf ("% s", str1 )! = EOF) {strcpy (str2, str1); // copy str1 to str2 printf ("% s \ n", str1, str2);} return 0 ;}
Note:: 1. str1 will overwrite str2 content; 2. Define an array that is equal to or greater than the length of str2.
You can also useStrncpyThe Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str1 [100], str2 [100]; 6 // one input string, end with a space of 7 while (scanf ("% s", str1, str2 )! = EOF) {8 strncpy (str2, str1, 3); // copy the first three characters of str1 to str1 9 printf ("% s \ n", str2 ); 10} 11 12 return 0; 13}
Note:: The first n characters of str2 will be overwritten by the first n characters of str1.
2. String connection
You can use the strcat function. The Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str1 [100], str2 [100]; 6 // one input string, end with a space of 7 while (scanf ("% s", str1, str2 )! = EOF) {8 strcat (str2, str1); // connect str1 to 9 printf ("% s \ n", str2) after str2; 10} 11 12 return 0; 13}
Note:: Note that the length of str2 is the sum of str1 and original str2, And the '\ 0' character at the end of str2 will automatically disappear.
You can also useStrncatFunction to connect the first n characters of a string to the end of another character. The Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str1 [100], str2 [100]; 6 // one input string, end with a space of 7 while (scanf ("% s", str1, str2 )! = EOF) {8 strncat (str2, str1, 3); // connect the first three characters of str1 to 9 printf ("% s \ n", str2) after str2 ); 10} 11 12 return 0; 13}
3. String comparison
AvailableStrcmpThe Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str1 [100], str2 [100]; 6 // one input string, end with a space of 7 while (scanf ("% s", str1, str2 )! = EOF) {8 int ptr = strcmp (str1, str2); // compare str1, str2 9 if (ptr <0) {// return value less than 0 10 printf ("% s <% s \ n", str1, str2); // str1 <str2 11} else if (ptr = 0) {// return value is equal to 012 printf ("% s = % s \ n", str1, str2 ); // str1 = str2 13} else {// return value greater than 014 printf ("% s> % s \ n", str1, str2 ); // str1> str215} 16} 17 18 return 0; 19}
4. String Length
You can use the strlen function. The Code is as follows:
1 # include <stdio. h> 2 # include <string. h> 3 4 int main () {5 char str1 [100]; 6 // one input string, ending with a space of 7 while (scanf ("% s", str1 )! = EOF) {8 // output String Length 9 printf ("strlen = % d \ n", strlen (str1); 10} 11 12 return 0; 13}