1. Preface
String is widely used in C language, because many data processing are text, that is, string, especially the text data returned by device interaction and web page interaction.
The string itself belongs to a character array, but it is different from the character array in that there is' \ 0 'at the end of the string. Because '\ 0' is specified at the end of the string, it is convenient to calculate the length, copy, find and splice.
2. Definition of string
char buff[]="I am a string"; char a[]="1234567890"; char b[]="abc"; char c[]={'a','b','c','\0'};
Adding a \ 0 at the end of an ordinary character array becomes a string.
3. Handle the case of letters in the string
Replace all uppercase letters in the string with lowercase letters. Or replace all lowercase letters with uppercase letters. It can be distinguished by formal parameters.
#include <stdio.h> #include <string.h> #include <stdlib.h> void func(char *str,int flag); int main() { char buff[100]; printf("Enter a string from the keyboard:"); scanf("%s",buff); printf("Source string:%s\n",buff); func(buff,0); printf("Convert uppercase to lowercase:%s\n",buff); func(buff,1); printf("Lowercase to uppercase:%s\n",buff); return 0; } //Function function: upper and lower case conversion //flag=0 means uppercase to lowercase = 1 means lowercase to uppercase void func(char *str,int flag) { int data; while(*str!='\0') { if(flag) { if(*str>='a'&& *str<='z') //a lowercase letter { *str=*str-32; } } else { if(*str>='A'&& *str<='Z') //a lowercase letter { *str=*str+32; } } str++; } }
4. Input two strings from the keyboard to judge whether they are equal
#include <stdio.h> int main() { char str1[100]; char str2[100]; int i=0; /*1. Input data*/ printf("Input string 1:"); scanf("%s",str1); printf("Input string 2:"); scanf("%s",str2); /*2. Compare strings*/ while(str1[i]!='\0'||str2[i]!='\0') { if(str1[i]!=str2[i])break; i++; } if(str1[i]=='\0'&&str2[i]=='\0') { printf("String equality.\n"); } else { printf("String inequality.\n"); } return 0; }
5. Enter a string from the keyboard and sort it from small to large
#include <stdio.h> #include <string.h> int main() { char str1[100]; int len=0; int i,j; int tmp; printf("Enter the string to sort:"); scanf("%s",str1); len=strlen(str1); //Start sorting for(i=0;i<len-1;i++) { for(j=0;j<len-1-i;j++) { if(str1[j]>str1[j+1]) { tmp=str1[j]; str1[j]=str1[j+1]; str1[j+1]=tmp; } } } printf("Sorted string:%s\n",str1); return 0; }
6. Input an integer from the keyboard: convert the integer to string output
For example: int a; scanf(“%d”,&a); Print out the value of a as a string.
#include <stdio.h> #include <string.h> int main() { char str[100]; char str1[100]; int data=0; int j=0,i=0; printf("Enter an integer from the keyboard:"); scanf("%d",&data); // 123 -->'1' '2' '3' while(data) { str[i++]=data%10+'0'; data=data/10; } for(j=0;j<i;j++) { str1[j]=str[i-j-1]; } str1[j]='\0'; printf("str1=%s\n",str1); return 0; }
7. Input a string from the keyboard and convert it to integer output
#include <stdio.h> #include <string.h> int main() { //"123" char str[100]; int data=0; int i=0; printf("Enter a string from the keyboard:"); scanf("%s",str); while(str[i]!='\0') { data*=10;//data=0 data=10 data=120 data+=str[i]-'0';//data=1 data=12 data=123 i++; } printf("data=%d\n",data); return 0; }
8. String deletion
Input a string from the keyboard, delete the specified word in the string, and output the result.
For example, the original string "akjbcds123dfjvbf123fdvbfd123"
Delete word: "123"
Output result: "akjbcdsdfjvbffdvbfd"
#include <stdio.h> #include <string.h> int main() { char str1[100]; char str2[100]; int i=0,j=0; int str2_len=0; /*1. Input data*/ printf("Input source string:"); scanf("%s",str1); printf("Enter the string to delete:"); scanf("%s",str2); /*2. Calculates the length of the string to be deleted*/ str2_len=strlen(str2); /*3. Find string*/ for(i=0;str1[i]!='\0';i++) { //Compare strings for(j=0;str2[j]!='\0';j++) { if(str1[i+j]!=str2[j])break; } if(str2[j]=='\0') { //4. Delete string --- overwrite from the back to the front for(j=i;str1[j]!='\0';j++) { str1[j]=str1[j+str2_len]; } str1[j]='\0'; i--; } } //5. Output results printf("str1=%s\n",str1); return 0; }
9. String insertion
Input a string from the keyboard, insert a string from the specified position, and then output the result.
For example, the original string "1234567890"
(1). Inserts a new word from the specified location. For example, insert an "ABC" string from the second subscript.
Result: "123ABC4567890"
#include <stdio.h> #include <string.h> int main() { char str1[100]; char str2[100]; int addr=0; int str1_len; int str2_len; int i; /*1. Input data*/ printf("Enter source string:"); scanf("%s",str1); printf("Enter the string to insert:"); scanf("%s",str2); printf("Enter the subscript position to insert:"); scanf("%d",&addr); str1_len=strlen(str1); //3 str2_len=strlen(str2); //2 /*2. Complete insertion*/ //Complete data movement for(i=str1_len-1;i>=addr;i--) { str1[i+str2_len]=str1[i]; } //Data replacement for(i=0;i<str2_len;i++) { str1[i+addr]=str2[i]; } str1[str1_len+str2_len]='\0'; /*3. output data*/ printf("str1=%s\n",str1); return 0; }
10. String substitution
Enter a string from the keyboard and replace the specified word with the desired word.
For example, the original string "123jfvfdj123dkfvbfdvdf"
Want to replace "123" with "888" or "8888" or "88"
#include <stdio.h> #include <string.h> int main() { char str1[100]; char str2[100]; char str3[100]; int str1_len=0; int str2_len=0; int str3_len=0; int i,j; int cnt=0; /*1.Prepare data*/ printf("Input source string:"); scanf("%s",str1); printf("Enter the string to find:"); scanf("%s",str2); printf("Enter a replacement string:"); scanf("%s",str3); /*2. Calculation length*/ str1_len=strlen(str1); str2_len=strlen(str2); str3_len=strlen(str3); /*3. String substitution*/ for(i=0;i<str1_len;i++) { //Find string for(j=0;j<str2_len;j++) { if(str1[i+j]!=str2[j])break; } //Replace if the search is successful if(j==str2_len) { //The total length is shorter if(str2_len>str3_len) { cnt=str2_len-str3_len; //Difference //Complete data move forward -- overwrite for(j=i+str2_len-cnt;j<str1_len;j++) { str1[j]=str1[j+cnt]; } str1[str1_len-cnt]='\0'; } //The total length is getting longer else if(str2_len<str3_len) { cnt=str3_len-str2_len; //Difference //Complete data move backward for(j=str1_len;j>=i+str2_len;j--) { str1[j+cnt]=str1[j]; } str1[str1_len+cnt]='\0'; } //replace for(j=0;j<str3_len;j++) { str1[i+j]=str3[j]; } //Recalculate length str1_len=strlen(str1); } } /*4. Complete string printing*/ printf("str1=%s\n",str1); return 0; }