• C语言的字符串处理函数


    常用函数

    C语言中有许多处理字符串的函数,这些函数在头文件中声明。以下是一些常用的字符串处理函数:
    1)strcmp(const char *str1, const char *str2): 用于比较两个字符串,如果相同,返回0,如果不同,返回一个非零值。

    #include   
    int main() {  
        char str1[15] = "Hello";  
        char str2[15] = "Hello";  
        int result = strcmp(str1, str2);  
        printf("Result: %d\n", result); // Result: 0  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2)strcpy(char *dest, const char *src): 用于复制一个字符串到另一个字符串。

    #include   
    int main() {  
        char str1[15] = "Hello";  
        char str2[15];  
        strcpy(str2, str1);  
        printf("str1: %s\n", str1); // str1: Hello  
        printf("str2: %s\n", str2); // str2: Hello  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3)strcat(char *dest, const char *src): 用于将一个字符串附加到另一个字符串的末尾

    #include   
    int main() {  
        char str1[20] = "Hello";  
        char str2[20] = " World";  
        strcat(str1, str2);  
        printf("%s\n", str1); // Hello World  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4)strlen(const char *str): 用于返回一个字符串的长度(不包括null字符)

    #include   
    int main() {  
        char str[15] = "Hello";  
        int len = strlen(str);  
        printf("Length: %d\n", len); // Length: 5  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5)sprintf(char *str, const char *format, …): 用于格式化一个字符串

    #include   
    #include   
    int main() {  
        char buffer[50];  
        float num = 3.14159;  
        sprintf(buffer, "The value of pi is %.2f", num);  
        printf("%s\n", buffer); // The value of pi is 3.14  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6)strstr(const char *haystack, const char *needle): 在字符串中查找子串

    #include   
    int main() {  
        char str[25] = "Hello world, world!";  
        char sub[7] = "world";  
        char *result = strstr(str, sub);  
        if(result != NULL) {  
            printf("Found '%s' in '%s': %ld\n", sub, str, result - str); // Found 'world' in 'Hello world, world!':(34 characters) 后面的数字表示子串在原字符串中的位置。  
        } else {  
            printf("'%s' not found in '%s'\n", sub, str); // 'world' not found in 'Hello world, world!'  
        }  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    7)int atoi(const char *str)它接受一个指向字符串的指针,并返回字符串表示的整数值。

    #include   
    #include   
      
    int main() {  
        char str[] = "12345";  
        int num = atoi(str);  
        printf("The number is %d\n", num);  
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这个例子中,字符串 “12345” 被转换为整数 12345。

    8)char *strtok(char *str, const char *delim),用于将字符串分解为一系列子串。它是基于指定的分隔符字符来分解字符串的

    #include   
    #include   
      
    int main() {  
        char str[] = "Hello,World,This,is,a,test";  
        char *token = strtok(str, ",");  
      
        while (token != NULL) {  
            printf("%s\n", token);  
            token = strtok(NULL, ",");  
        }  
      
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    9)int sprintf_s(char *str, size_t size, const char *format, …);sprintf_s 是一个安全版本的 sprintf 函数,用于将格式化的数据写入字符串中。与 sprintf 不同,sprintf_s 允许指定目标缓冲区的最大大小,从而避免了缓冲区溢出的问题。

    #include   
    #include   
    #include   
      
    int main() {  
        char buffer[50];  
        int a = 10;  
        float b = 3.14;  
        size_t bufferSize = sizeof(buffer);  
      
        sprintf_s(buffer, bufferSize, "The values are %d and %f", a, b);  
        printf("Output: %s\n", buffer);  
      
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    10)int sscanf(const char *str, const char *format, …);于从字符串中按照指定格式读取数据。其中,str 是要解析的字符串,format 是指定输入格式的字符串。变长参数列表(在 … 中)允许你指定要读取的变量,以便将解析后的值存储到这些变量中。

    #include   
      
    int main() {  
        char str[] = "John 25 1.75";  
        char name[50];  
        int age;  
        double height;  
      
        sscanf(str, "%s %d %lf", name, &age, &height);  
      
        printf("Name: %s\n", name);  
        printf("Age: %d\n", age);  
        printf("Height: %.2lf\n", height);  
      
        return 0;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在上面的示例中,我们使用 sscanf 从字符串 str 中解析出姓名、年龄和身高,并将这些值分别存储到 name、age 和 height 变量中。然后,我们使用 printf 函数将解析后的值输出到控制台上。这里的格式字符串 “%s %d %lf” 指定了要读取的字段顺序和类型。

  • 相关阅读:
    SentinelResource注解详解
    数据结构之栈的讲解
    进程同步互斥之吸烟者问题,读者写者问题,哲学家进餐问题
    Java注解系统学习与实战
    Hadoop启动后jps发现没有DateNode解决办法
    大数据之LibrA数据库系统服务部署原则及运行环境要求
    【组件库】element-plus组件库
    6-2 顺序表操作集分数 20
    解决传统难题,WMS系统实现信息数据实时追踪
    【校招VIP】产品项目分析之功能分析
  • 原文地址:https://blog.csdn.net/techenliu/article/details/133753840