【Linux C】字符串处理相关函数

字符串处理相关函数

  1. sizeofstrlen的区别:
  • sizeof 是 C 语言内置的操作符关键字,而 strlen 是 C 语言库函数;
  • sizeof 仅用于计算数据类型的大小或者变量的大小,而 strlen 只能以结尾为’ \0 '的字符串作为参数;
  • 编译器在编译时就计算出了 sizeof 的结果,而 strlen 必须在运行时才能计算出来;
  • sizeof 计算数据类型或变量会占用内存的大小,strlen 计算字符串实际长度。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
     char str[50] = "Linux app strlen test!";
     char *ptr = str;
     printf("sizeof: %ld\n", sizeof(str));
     printf("strlen: %ld\n", strlen(str));
     puts("~~~~~~~~~~");
     printf("sizeof: %ld\n", sizeof(ptr));
     printf("strlen: %ld\n", strlen(ptr));
     exit(0);
}
  1. strcat()函数和 strncat()函数

    strcat()函数会把 src 所指向的字符串追加到 dest 所指向的字符串末尾,所以必须要保证 dest 有足够的存储空间来容纳两个字符串,否则会导致溢出错误;dest 末尾的’ \0 ‘结束字符会被覆盖,src 末尾的结束字符’ \0 ‘会一起被复制过去,最终的字符串只有一个’ \0 '。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
         char str1[100] = "Linux app strcat test, ";
         char str2[] = "Hello World!";
         strcat(str1, str2);
         puts(str1);
         exit(0);
    }
    

    strncat()strcat()的区别在于,strncat 可以指定源字符串追加到目标字符串的字符数量。如果源字符串 src 包含 n 个或更多个字符,则 strncat()将 n+1 个字节追加到 dest 目标字符串(src 中的 n个字符加上结束字符’ \0 ')。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char str1[100] = "Linux app strcat test, ";
         char str2[] = "Hello World!";
         strncat(str1, str2, 5);
         puts(str1);
         exit(0);
    }
    
  2. strcpy()函数和 strncpy()函数

    strcpy()会把 src(必须包含结束字符’ \0 ‘)指向的字符串复制(包括字符串结束字符’ \0 ')到 dest,所以必须保证 dest 指向的内存空间足够大,能够容纳下 src 字符串,否则会导致溢出错误。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char str1[100] = {0};
         char str2[] = "Hello World!";
         strcpy(str1, str2);
         puts(str1);
         exit(0);
    }
    

    strncpy()strcpy()的区别在于,strncpy()可以指定从源字符串 src 复制到目标字符串 dest 的字符数量。把 src 所指向的字符串复制到 dest,最多复制 n 个字符。当 n 小于或等于 src 字符串长度(不包括结束字符的长度)时,则复制过去的字符串中没有包含结束字符’ \0 ‘;当 n 大于 src 字符串长度时,则会将 src 字符串的结束字符’ \0 '也一并拷贝过去,必须保证 dest 指向的内存空间足够大,能够容纳下拷贝过来的字符串,否则会导致溢出错误。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char str1[100] = "AAAAAAAAAAAAAAAAAAAAAAAA";
         char str2[] = "Hello World!";
         strncpy(str1, str2, 5);
         puts(str1);
         puts("~~~~~~~~~~~~~~~");
         strncpy(str1, str2, 20);
         puts(str1);
         exit(0);
    }
    
  3. memset 函数

    memset()函数用于将某一块内存的数据全部设置为指定的值。参数 c 虽然是以 int 类型传递,但 memset()函数在填充内存块时是使用该值的无符号字符形式,也就是函数内部会将该值转换为 unsigned char 类型的数据,以字节为单位进行数据填充。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char str[100];
         memset(str, 0x0, sizeof(str));
         exit(0);
    }
    
  4. strcmp()strncmp()函数

    int strcmp(const char *s1, const char *s2);

    • 如果返回值小于 0,则表示 str1 小于 str2
    • 如果返回值大于 0,则表示 str1 大于 str2
    • 如果返回值等于 0,则表示字符串 str1 等于字符串 str2

    strcmp 进行字符串比较,主要是通过比较字符串中的字符对应的 ASCII 码值,strcmp 会根据 ASCII 编码依次比较 str1str2 的每一个字符,直到出现了不同的字符,或者某一字符串已经到达末尾(遇见了字符串结束字符’ \0 ')。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         printf("%d\n", strcmp("ABC", "ABC"));
         printf("%d\n", strcmp("ABC", "a"));
         printf("%d\n", strcmp("a", "ABC"));
         exit(0);
    }
    

    strncmp()strcmp()函数一样,也用于对字符串进行比较操作,但最多比较前 n 个字符。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         printf("%d\n", strncmp("ABC", "ABC", 3));
         printf("%d\n", strncmp("ABC", "ABCD", 3));
         printf("%d\n", strncmp("ABC", "ABCD", 4));
         exit(0);
    }
    
  5. strchr 函数

    使用 strchr()函数可以查找到给定字符串当中的某一个字符。

    字符串结束字符’ \0 ‘也将作为字符串的一部分,因此,如果将参数 c 指定为’ \0 ',则函数将返回指向结束字符的指针。strchr 函数在字符串 s 中从前到后(或者称为从左到右)查找字符 c,找到字符 c 第一次出现的位置就返回,返回值指向这个位置,如果找不到字符 c 就返回 NULL。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char *ptr = NULL;
         char str[] = "Hello World!";
         ptr = strchr(str, 'W');
         if (NULL != ptr) {
         printf("Character: %c\n", *ptr);
         printf("Offset: %ld\n", ptr - str);
         }
         exit(0);
    }
    
  6. strrchr 函数

    strrchr()strchr()函数一样,它同样表示在字符串中查找某一个字符,返回字符第一次在字符串中出现的位置,如果没找到该字符,则返回值 NULL,但两者唯一不同的是,strrchr()函数在字符串中是从后到前(或者称为从右向左)查找字符,找到字符第一次出现的位置就返回,返回值指向这个位置。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char *ptr = NULL;
         char str[] = "I love my home";
         ptr = strchr(str, 'o');
         if (NULL != ptr)
         printf("strchr: %ld\n", ptr - str);
         ptr = strrchr(str, 'o');
         if (NULL != ptr)
         printf("strrchr: %ld\n", ptr - str);
         exit(0);
    }
    
  7. strstr 函数

    char *strstr(const char *haystack, const char *needle);

    strchr()函数不同的是,strstr()可在给定的字符串 haystack 中查找第一次出现子字符串 needle 的位置,不包含结束字符’ \0 '。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char *ptr = NULL;
         char str[] = "I love my home";
         ptr = strstr(str, "home");
         if (NULL != ptr) {
         printf("String: %s\n", ptr);
         printf("Offset: %ld\n", ptr - str);
         }
         exit(0);
    }
    
  8. atoiatolatoll函数

    atoi()atol()atoll()三个函数可用于将字符串分别转换为 intlong int 以及 long long 类型的数据。使用这些函数需要包含头文件<stdlib.h>

    使用 atoi()atol()atoll()函数只能转换十进制表示的数字字符串,即 0~9。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         printf("atoi: %d\n", atoi("500"));
         printf("atol: %ld\n", atol("500"));
         printf("atoll: %lld\n", atoll("500"));
         exit(0);
    }
    
  9. strtolstrtoll函数

    strtol()strtoll()两个函数可分别将字符串转为 long int 类型数据和 long long ing 类型数据,与 atol()、atoll()之间的区别在于,strtol()strtoll()可以实现将多种不同进制数(譬如二进制表示的数字字符串、八进制表示的数字字符串、十六进制表示的数数字符串)表示的字符串转换为整形数据。

    需要进行转换的目标字符串可以以任意数量的空格或者 0 开头,转换时跳过前面的空格字符,直到遇上数字字符或正负符号(’ + ‘或’ - ‘)才开始做转换,而再遇到非数字或字符串结束时(’ /0 ')才结束转换,并将结果返回。

    在 base=0 的情况下,如果字符串包含一个了“0x”前缀,表示该数字将以 16 为基数;如果包含的是“0”前缀,表示该数字将以 8 为基数。

    当 base=16 时,字符串可以使用“0x”前缀。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         printf("strtol: %ld\n", strtol("0x500", NULL, 16));
         printf("strtol: %ld\n", strtol("0x500", NULL, 0));
         printf("strtol: %ld\n", strtol("500", NULL, 16));
         printf("strtol: %ld\n", strtol("0777", NULL, 8));
         printf("strtol: %ld\n", strtol("0777", NULL, 0));
         printf("strtol: %ld\n", strtol("1111", NULL, 2));
         printf("strtol: %ld\n", strtol("-1111", NULL, 2));
         exit(0);
    }
    
  10. strtoulstrtoull函数

    这两个函数使用方法与 strtol()、strtoll()一样,就是返回值不同。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         printf("strtoul: %lu\n", strtoul("0x500", NULL, 16));
         printf("strtoul: %lu\n", strtoul("0x500", NULL, 0));
         printf("strtoul: %lu\n", strtoul("500", NULL, 16));
         printf("strtoul: %lu\n", strtoul("0777", NULL, 8));
         printf("strtoul: %lu\n", strtoul("0777", NULL, 0));
         printf("strtoul: %lu\n", strtoul("1111", NULL, 2));
         exit(0);
    }
    
  11. atof 函数

    atof()用于将字符串转换为一个 double 类型的浮点数据。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
        printf("atof: %lf\n", atof("0.123"));
         printf("atof: %lf\n", atof("-1.1185"));
         printf("atof: %lf\n", atof("100.0123"));
         exit(0);
    }
    
  12. strtodstrtofstrtold 函数、

    strtof()strtod()以及 strtold()三个库函数可分别将字符串转换为 float 类型数据、double 类型数据、longdouble 类型数据。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         printf("strtof: %f\n", strtof("0.123", NULL));
         printf("strtod: %lf\n", strtod("-1.1185", NULL));
         printf("strtold: %Lf\n", strtold("100.0123", NULL));
         exit(0);
    }
    
  13. sprintf()snprintf()函数

    数字转换为字符串。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
         char str[20] = {0};
         sprintf(str, "%d", 500);
         puts(str);
         memset(str, 0x0, sizeof(str));
         sprintf(str, "%f", 500.111);
         puts(str);
         memset(str, 0x0, sizeof(str));
         sprintf(str, "%u", 500);
         puts(str);
         exit(0);
    }
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值