1 Introduction
Strings are used a lot in C language, because a lot of data processing is text, that is, strings, especially device interaction and web page interaction return almost all text data.
The string itself belongs to the character array, but the difference from the character array is that the string ends with '\0'. Because the string has a '\0' at the end, it is very convenient for calculating the length, copying, searching, and splicing.
2. Definition of string
char buff[]="i is 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 a string with lowercase letters. Or replace all lowercase letters with uppercase letters. 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: uppercase and lowercase conversion //flag=0 means convert uppercase to lowercase =1 means convert lowercase to uppercase void func(char *str,int flag) { int data; while(*str!='\0') { if(flag) { if(*str>='a'&& *str<='z') //lower case { *str=*str-32; } } else { if(*str>='A'&& *str<='Z') //lower case { *str=*str+32; } } str++; } }
4. Enter 2 character strings from the keyboard to determine 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 equal.\n"); } else { printf("strings are not equal.\n"); } return 0; }
5. Enter a string from the keyboard and sort it in ascending order
#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; }
- Input an integer from the keyboard: convert the integer to a string output
for example:
int a; scanf("%d",&a);
Print out the value of a in string form.
#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 an 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
Enter 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. Calculate the length of the string to remove*/ 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 the 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 the result printf("str1=%s\n",str1); return 0; }
9. String insertion
Enter a string from the keyboard, insert a string from the specified position, and output the result.
For example: the original string "1234567890"
(1). Insert a new word from the specified position. 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("input source string:"); scanf("%s",str1); printf("Enter the string to be inserted:"); 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 Replacement
Enter a character 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 search string:"); scanf("%s",str2); printf("Enter the replacement string:"); scanf("%s",str3); /*2. Calculate length*/ str1_len=strlen(str1); str2_len=strlen(str2); str3_len=strlen(str3); /*3. string replacement*/ 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 found if(j==str2_len) { //The overall length is shortened if(str2_len>str3_len) { cnt=str2_len-str3_len; //difference //Complete data forward movement -- overwrite for(j=i+str2_len-cnt;j<str1_len;j++) { str1[j]=str1[j+cnt]; } str1[str1_len-cnt]='\0'; } //The overall length has become longer else if(str2_len<str3_len) { cnt=str3_len-str2_len; //difference //complete data move backwards 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; }