• C语言字符串从入门到进阶指南


    目录

    字符数组 和 字符串区别

    字符数组 

    字符串 

    统计字符串每个字符出现的次数

    字符串获取 scanf

    gets

    fgets

    puts

    fputs

    strlen

    求非空字符串元素个数

    判断字符串是否回文

    字符串处理函数   

    字符串拷贝: 

     strcpy 

    strncpy

    字符串拼接

     strcat

     strncat 

    字符串比较   

     strcmp

     strncmp

    字符串格式化输入、输出

    sprintf

    字符串查找字符子串

     strchr()

     strrchr()

      strstr()

    字符串分割

    strtok()

    字符串转化成浮点数

     atoi/atof/atol


    字符数组 和 字符串区别

     字符数组 

     char str[5] = {'h', 'e', 'l', 'l', 'o'};    

    字符串 

     char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};

     char str[6] = "hello";

     printf("%s");    使用printf打印字符串的时候,必须碰到 \0 结束。

     

    统计字符串每个字符出现的次数

    1. for (size_t i = 0; i < 10; i++)
    2. {
    3. scanf("%c", &str[i]);
    4. }
    5. int count[26] = {0}; // 代表26个英文字母出现的次数。
    6. for (size_t i = 0; i < 11; i++)
    7. {
    8. int index = str[i] - 'a'; // 用户输入的字符在 count数组中的下标值。
    9. count[index]++;
    10. }
    11. for (size_t i = 0; i < 26; i++)
    12. {
    13. if (count[i] != 0)
    14. {
    15. printf("%c字符在字符串中出现 %d 次\n", i+'a', count[i]);
    16. }
    17. }



    字符串获取 scanf

     1)用于存储字符串的空间必须足够大,防止溢出。 char str[5];

      2) 获取字符串,%s, 遇到空格 和 \n 终止。

     借助“正则表达式”, 获取带有空格的字符串:scanf("%[^\n]", str);

    gets

    从键盘获取一个字符串, 返回字符串的首地址。 可以获取带有 空格的字符串。 【不安全】

     char *gets(char *s);

     参数:用来存储字符串的空间地址。

     返回值:返回实际获取到的字符串首地址。

    代码

    如果输入 ni hao  输出的就是 获取的字符串为ni hao

    1. char str[10];
    2. printf("获取的字符串为:%s", gets(str));
    3. system("pause");
    4. return EXIT_SUCCESS;

     

    fgets

     从stdin获取一个字符串, 预留 \0 的存储空间。空间足够读 \n, 空间不足舍弃 \n  【安全】

     char *fgets(char *s, int size, FILE *stream);

     参1:用来存储字符串的空间地址。

     参2:描述空间的大小。

     参3:读取字符串的位置。    键盘 --》 标准输入:std

     返回值:返回实际获取到的字符串首地址。

    代码

    如果输入hello world 输出hello wor

    1. int main(void)
    2. {
    3. char str[10];
    4. printf("获取的字符串为:%s", fgets(str, sizeof(str), stdin));
    5. system("pause");
    6. return EXIT_SUCCESS;
    7. }


    puts

    将一个字符串写出到屏幕. printf("%s", "hello"); / printf("hello\n"); / puts("hello");   输出字符串后会自动添加 \n 换行符。

     int puts(const char *s);    

     参1:待写出到屏幕的字符串。

     返回值: 成功:非负数 0。 失败: -1.    

    代码

    1. int main(void)
    2. {
    3. char str[] = "hello world\n";
    4. int ret = puts(str); // puts("hello world");
    5. printf("ret = %d\n", ret); //ret = 0
    6. system("pause");
    7. return EXIT_SUCCESS;
    8. }


    fputs

     将一个字符串写出到stdout.输出字符串后, 不添加 \n 换行符。

     int fputs(const char * str, FILE * stream);    

     参1:待写出到屏幕的字符串。        屏幕 --》标准输出: stdout

     参数:写出位置 stdout

     返回值: 成功:0。 失败: -1.

    代码

    1. int main(void)
    2. {
    3. char str[] = "hello world\n";
    4. //int ret = fputs(str, stdout); //
    5. int ret = fputs("hello world\n", stdout);
    6. printf("ret = %d\n", ret);
    7. system("pause");
    8. return EXIT_SUCCESS;
    9. }

    strlen

    碰到 \0 结束,不包含'\0'

    size_t strlen(const char *s);

    参1: 待求长度的字符串

    返回:有效的字符个数。   

    代码

    1. int main(void)
    2. {
    3. char str[] = "hello world";
    4. printf("sizeof(str) = %u\n", sizeof(str)); //输出12
    5. printf("strlen(str) = %u\n", strlen(str)); //输出11
    6. system("pause");
    7. return EXIT_SUCCESS;
    8. }

     

    求非空字符串元素个数

    1. #include
    2. #include
    3. #include
    4. #include
    5. int no_space_str(char *str)
    6. {
    7. int count = 0;
    8. //har str[] = "ni chou sha ";
    9. char* p = str;
    10. while (*p)
    11. {
    12. if (*p != ' ')
    13. {
    14. count++;
    15. }
    16. p++;
    17. }
    18. return count++;
    19. }
    20. int main()
    21. {
    22. char str[] = "ni chou sha ";
    23. int ret = no_space_str(str);
    24. printf("%d", ret);
    25. }


    判断字符串是否回文

    1. #include
    2. #include
    3. #include
    4. #include
    5. //字符串逆置
    6. void str_inserse(char* str)
    7. {
    8. char* start = str; //记录元素首地址
    9. char* end = str + strlen(str) - 1; // 记录最后一个元素地址
    10. while (start < end) //首元素地址是否小于最后一个元素地址
    11. {
    12. char temp = *start;
    13. *start = *end;
    14. *end = temp;
    15. start++; //首元素对应指针后移
    16. end--; //尾元素对应指针前移
    17. }
    18. }
    19. //判断回文字符串 abcddcba
    20. int str_abcba(char *str)
    21. {
    22. char *start = str;
    23. char *end = str + strlen(str) - 1;
    24. while (start < end)
    25. {
    26. if (*start != *end) //判断字符是否一致
    27. {
    28. return 0; // 0表示非回文
    29. }
    30. start++;
    31. end--;
    32. }
    33. return 1;
    34. }
    35. int main()
    36. {
    37. char str[] = "this is a test";
    38. //str_inserse(str);
    39. //printf("str = %s----------------------\n", str);
    40. char s2[] = "abccba";
    41. int ret = str_abcba(s2);
    42. if (ret == 0) printf("不是回文\n");
    43. else if (ret == 1) printf("是回文\n");
    44. }


    字符串处理函数   

    头文件 #include

    字符串拷贝: 

     strcpy 

     将 src 的内容,拷贝给 dest。 返回 dest。 保证dest空间足够大。【不安全】

     char *strcpy(char *dest, const char *src);

     函数调用结束 返回值和 dest参数结果一致。

    如果参数dest所指的内存空间不够大,可能会造成缓冲溢出的错误情况 

    strncpy

    参数

    dest:目的的字符串首地址

    src:原字符首要地址

    n:指定需要拷贝字符串个数

    将 src 的内容,拷贝给 dest。只拷贝 n 个字节。 通常 n 与dest对应的空间一致。 

    默认 不添加 ‘\0’

    char *strncpy(char *dest, const char *src, size_t n);    

    特性: n > src: 只拷贝 src 的大小

                n < src: 只拷贝 n 字节大小。 不添加 ‘\0’

    成功返回字符串dest首地址 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. // strcpy
    7. int main0301(void)
    8. {
    9. char src[] = "abc efg zhansan wangwu ";
    10. char dest[10] = {0};
    11. char *p = strcpy(dest, src); ;// 字符串src 拷贝给dest
    12. printf("p= %s\n", p);
    13. printf("dest = %s\n", dest);
    14. system("pause");
    15. return EXIT_SUCCESS;
    16. }
    17. // strncpy
    18. int main(void)
    19. {
    20. char src[] = "hello world";
    21. char dest[100] = { 0 };
    22. char *p = strncpy(dest, src, 100); ;// 字符串src 拷贝给dest
    23. for (size_t i = 0; i < 10; i++)
    24. {
    25. printf("%c\n", p[i]);
    26. }
    27. printf("p= %s\n", p);
    28. printf("dest = %s\n", dest);
    29. system("pause");
    30. return EXIT_SUCCESS;
    31. }


      

    字符串拼接

     strcat

    将 src 的内容,拼接到 dest 后。 返回拼接后的字符串。    保证 dest 空间足够大。

     char *strcat(char *dest, const char *src);       

     strncat 

    将 src 的前 n 个字符,拼接到 dest 后。 形成一个新的字符串。保证 dest 空间足够大。

    char *strncat(char *dest, const char *src, size_t n);

     函数调用结束 返回值和 dest 参数结果一致。

    代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. int main0401(void)
    7. {
    8. char src[] = "world";
    9. char dest[] = "hello";
    10. char *p = strcat(dest, src);
    11. printf("p = %s\n", p);
    12. printf("dest = %s\n", dest); // helloworld
    13. system("pause");
    14. return EXIT_SUCCESS;
    15. }
    16. int main0402(void)
    17. {
    18. char src[] = "world";
    19. char dest[6] = "hello";
    20. char *p = strncat(dest, src, 3);
    21. printf("p = %s\n", p);
    22. printf("dest = %s\n", dest);
    23. printf("%d\n", strlen(dest));
    24. system("pause");
    25. return EXIT_SUCCESS;
    26. }


    字符串比较   

    不能使用 > < >= <= == != 

     strcmp

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

    比较s1和s2两个字符串,如果相等 返回0.如果不相等,进一步表 s1 和 s2 对应位 ASCII码   值。

                s1 > s2 返回1

                s1 < s2 返回-1

     strncmp

     int strncmp(const char *s1, const char *s2, size_t n);

     比较s1和s2两个字符串的前n个字符,

     如果相等 返回0。如果不相等,进一步表 s1 和 s2 对应位 ASCII码值。(不比字符串             ASCII码的和)

      s1 > s2 返回1

      s1 < s2 返回-1        

     代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. int main0501(void)
    7. {
    8. char *str1 = "helloworld";
    9. char *str2 = "helloz";
    10. printf("ret = %d\n", strcmp(str1, str2));
    11. system("pause");
    12. return EXIT_SUCCESS;
    13. }
    14. int main0502(void)
    15. {
    16. char *str1 = "helloworld";
    17. char *str2 = "helloz";
    18. printf("ret = %d\n", strncmp(str1, str2, 8));
    19. system("pause");
    20. return EXIT_SUCCESS;
    21. }


    字符串格式化输入、输出

    如果相输入带空格字符串scanf("%[^\n]", str);

    sprintf

    int sprintf(char *str, const char *format, ...);

    功能:根据参数format字符串来转化并格式化数据,然后将结果输出到str指定的空间,直到字符串出现结束符 '/0' 为止

    参数:

    str 字符串首地址

    format:字符串格式,用法和printf()一样

    sscanf()

    int sscanf(const char *str, const char *format, ...);

    功能:从str指定的字符串读取数据,并根据参数fornat字符串来转化并格式化数据

    参数:

          str:指定的字符串首地址

          format:字符串格式,用法和scanf()一样

    代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. int main0401(void)
    7. {
    8. char src[] = "world";
    9. char dest[] = "hello";
    10. char *p = strcat(dest, src);
    11. printf("p = %s\n", p);
    12. printf("dest = %s\n", dest); // helloworld
    13. system("pause");
    14. return EXIT_SUCCESS;
    15. }
    16. int main0402(void)
    17. {
    18. char src[] = "world";
    19. char dest[6] = "hello";
    20. char *p = strncat(dest, src, 3);
    21. printf("p = %s\n", p);
    22. printf("dest = %s\n", dest);
    23. printf("%d\n", strlen(dest));
    24. system("pause");
    25. return EXIT_SUCCESS;
    26. }


    字符串查找字符子串


     strchr()

    在字符串str中 找一个字符出现的位置。 返回字符在字符串中的地址。

    char *strchr(const char *s, int c);

    printf("%s\n" strchr("hehehahahoho", 'a'));  --> "ahahoho"

     strrchr()

     自右向左,在字符串str中 找一个字符出现的位置。 返回字符在字符串中的地址。

     char *strrchr(const char *s, int c);

     printf("%s\n" strrchr("hehehahahoho", 'a'));  --> "ahoho"         

      strstr()

     在字符串str中,找子串substr第一次出现的位置。返回地址。

     char *strstr(const char *str, const char *substr);

     在字符串中找子串的位置。

      printf("%s\n" strrchr("hehehahahoho", "ho"));  --> "hoho"

      printf("%s\n" strrchr("hehehahahoho", "xixi"));  --> NULL        

    字符串分割

    strtok()

    按照既定的分割符,来拆分字符串,按 ‘ . ’   “www.baidu.com”  使用方法strtok("www.baidu.com","k");  -->变成

    char *strtok(char *str, const char *delim);

                参1: 待拆分字符串

                参2: 分割符组成的“分割串”

    返回:字符串拆分后的首地址。 “拆分”:将分割字符用 '\0'替换。

    特性:
            1)strtok拆分字符串是直接在 原串 上操作,所以要求参1必须,可读可写(char *str = "www.baidu.com" 不行!!!)

            2)第一次拆分,参1 传待拆分的原串。    第1+ 次拆分时,参1传 NULL.

     代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. int main0701(void)
    7. {
    8. char str[] = "www.itcast.cn.com.net"; // www itcast cn
    9. char *p = strtok(str, "."); // 第一次拆分,参1 传 待拆分的原串。
    10. while (p != NULL)
    11. {
    12. p = strtok(NULL, "."); // 第1+ 次拆分是,参1传 NULL.
    13. printf("%s\n", p); //输出itcast cn com net (null)
    14. }
    15. system("pause");
    16. return EXIT_SUCCESS;
    17. }

    字符串转化成浮点数


     atoi/atof/atol

         
     使用这类函数进行转换,要求,原串必须是可转换的字符串。

     错误使用:"abc123" --> 0;    "12abc345" ---> 12;  "123xyz" -->123
         

     atoi:字符串 转 整数。 int atoi(const char *nptr);

     atof:字符串 转 浮点数

     atol:字符串 转 长整数     

    代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. static int a = 1034673;
    7. void test1(void)
    8. {
    9. static int b = 0;
    10. printf("b = %d\n", b++);
    11. }
    12. int main0801(void)
    13. {
    14. char str[] = "abc345";
    15. int num = atoi(str);
    16. printf("num = %d\n", num);
    17. char str1[] = " -10";
    18. int num1 = atoi(str1);
    19. printf("num1 = %d\n", num1);
    20. char str2[] = "0.123f";
    21. double num2 = atof(str2);
    22. printf("num2 = %.2lf\n", num2);
    23. char str3[] = "123L";
    24. long num3 = atol(str3);
    25. printf("num3 = %ld\n", num3);
    26. system("pause");
    27. return EXIT_SUCCESS;
    28. }

  • 相关阅读:
    图扑软件 3D 组态编辑器,低代码零代码构建数字孪生工厂
    14 SpringMVC执行流程
    Folium笔记:HeatMap
    创建spring boot后启动报错: Failed to bind properties under ‘spring.datasource‘
    中英文双语言海外微盘交易源码 微盘交易源码 虚拟币微盘交易系统源码 支持产品数据
    使用两个栈实现一个队列
    C++多态技术
    直冲云霄,阿里大牛耗时49天整理12W字面试手册,押题准确率直冲95%
    Echarts 教程一
    串口数据包收发
  • 原文地址:https://blog.csdn.net/qq_64691289/article/details/127637706