1. Introduction

Strings are used a lot in C because a lot of data processing is text, that is strings, especially device interactions and Web page interactions that return almost all text data.

The string itself is a character array, except that it ends with a ‘\0’. Because strings are specified to end with ‘\0’, it is convenient to calculate length, copy, find, and concatenate operations.

2. String definition

char buff[]="I am a string";
char a[]="1234567890";
char b[]="abc";
char c[]={'a'.'b'.'c'.'\ 0'};
Copy the code

Adding a \0 to the end of an ordinary character array becomes a string.

3. Handle letter case in string

Replace all uppercase letters in a string with lowercase letters. Or all lowercase letters can be uppercase. It can be distinguished by parameters.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(char *str,int flag);
int main(a)
{
    char buff[100];
    printf("Input string from keyboard :");
    scanf("%s",buff);
    printf("Source string :%s\n",buff);
    func(buff,0);
    printf("Uppercase conversion to lowercase :%s\n",buff);
    func(buff,1);
    printf("From lowercase to uppercase :%s\n",buff);
    return 0;
}
​
// Converts uppercase to lowercase
//flag=0: uppercase conversion. Lowercase =1: lowercase conversion
void func(char *str,int flag)
{
    int data;
    while(*str! ='\ 0')
    {
        if(flag)
        {
            if(*str>='a'&& *str<='z') / / lowercase
            {
                *str=*str- 32; }}else
        {
            if(*str>='A'&& *str<='Z') / / lowercase
            {
                *str=*str+32; } } str++; }}Copy the code

4. Enter two strings on the keyboard and check whether they are the same

#include <stdio.h>
int main(a)
{
    char str1[100];
    char str2[100];
    int i=0;
    /*1. Input data */
    printf("Enter string 1:");
    scanf("%s",str1);
    printf("Enter string 2:");
    scanf("%s",str2);
    /*2. Compare the string */
    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;
}
Copy the code

5. Type a string from the keyboard and sort it in descending order

#include <stdio.h>
#include <string.h>int main(a)
{
    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;
}
Copy the code

7. Input a string from the keyboard and output it as an integer

#include <stdio.h>
#include <string.h>
int main(a)
{
    / / "123"
    char str[100];
    int data=0;
    int i=0;
    printf("Input string from 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;
}
Copy the code

8. Delete the string

Type a string from the keyboard, delete the specified word in the string, and print the result.

For example: the original string “akjbcds123dfjvbf123fdvbfd123”

Delete words: “123”

Output: “akjbcdsdfJvbffDVBFD”

#include <stdio.h>
#include <string.h>int main(a)
{
    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 be deleted */
    str2_len=strlen(str2);
                
    /*3. Find the 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 -- back to front overwrite
            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;
}
Copy the code

9. String insertion

Type a string from the keyboard, insert a string from the specified position, and print 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.

Results: 123 abc4567890 “”

#include <stdio.h>
#include <string.h>int main(a)
{
    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("Input 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. Insert */
    // Complete the 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;
}
Copy the code

10. String replacement

Type a string from the keyboard, replacing 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(a)
{
    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 the length */
    str1_len=strlen(str1);
    str2_len=strlen(str2);
    str3_len=strlen(str3);
    /*3. Replace */ with string
    for(i=0; i<str1_len; i++) {// Find the string
        for(j=0; j<str2_len; j++) {if(str1[i+j]! =str2[j])break;
        }
        // If the search succeeds, replace it
        if(j==str2_len)
        {
            // The total length is shorter
            if(str2_len>str3_len)
            {
                cnt=str2_len-str3_len; / / difference
                // Complete data moving 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 longer
            else if(str2_len<str3_len)
            {
                cnt=str3_len-str2_len; / / difference
                // Finish moving the data 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 the length
            str1_len=strlen(str1); }}/*4. Finish printing the string */
    printf("str1=%s\n",str1);
    return 0;
}
Copy the code