1.while模型

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. //求一个字符串中某个子串出现的次数
  7. int getCout(char *str, char *substr, int *count)
  8. {
  9. int rv = ;
  10. char *p = str;
  11.  
  12. int ncout = ;
  13. if (str==NULL || substr== NULL || count==NULL)
  14. {
  15. rv = -;
  16. printf("func getCout()check (str==NULL || substr== NULL || count==NULL) err:%d \n" , rv);
  17. return rv;
  18. }
  19. while (*p != '\0'){
  20. p = strstr(p, substr);
  21. if (p == NULL)
  22. {
  23. break;
  24. }
  25. else
  26. {
  27. ncout++;
  28. p = p + strlen(substr);
  29. }
  30.  
  31. } ;
  32. //通过指针把结果传出来
  33. *count = ncout;
  34. return rv;
  35. }
  36.  
  37. int main()
  38. {
  39. int ret = ;
  40. char *p = "abcd1111abcd222abcd3333";
  41. char *subp = "abcd";
  42. int ncout = ;
  43.  
  44. ret = getCout(p, subp, &ncout);
  45. if (ret != )
  46. {
  47. printf("func getCout() err:%d \n", ret);
  48. return ;
  49. }
  50. printf("coutn = %d \n", ncout);
  51. return ;
  52. }

2.两头堵模型:两种写法

  1. //求去掉两边空格之后的字符串长度,指针作为形参传入,将结果赋值给指针指向的内存
  2. int trimSpaceStr01(char *p, int *mycount)
  3. {
  4. int ret = ;
  5.  
  6. int ncount = ;
  7. int i= , j;
  8. j = strlen(p) - ;
  9.  
  10. while (isspace(p[i]) && p[i] != '\0')
  11. {
  12. i++;
  13. }
  14.  
  15. while (isspace(p[j]) && j>)
  16. {
  17. j--;
  18. }
  19.  
  20. ncount = j - i + ;
  21. *mycount = ncount;
  22. return ret;
  23. }
  24.  
  25. //求去掉两边空格之后的字符串,将指针作为形参传入,将结果赋值给形参指向的内存空间
  26. int trimSpaceStr2(char *p, char *buf)
  27. {
  28. int ret = ;
  29.  
  30. int ncount = ;
  31. int i, j;
  32. i = ;
  33. j = strlen(p) - ;
  34.  
  35. while (isspace(p[i]) && p[i] != '\0')
  36. {
  37. i++;
  38. }
  39.  
  40. while (isspace(p[j]) && j>)
  41. {
  42. j--;
  43. }
  44.  
  45. ncount = j - i + ;
  46. //
  47. strncpy(buf, p + i, ncount);
  48. buf[ncount] = '\0';
  49. return ret;
  50. }
  51.  
  52. //这种写法不好
  53. //不要轻易去改变指针输入特性中in内存块的内存
  54. int trimSpaceStr2_notgood(char *p)
  55. {
  56. int ret = ;
  57.  
  58. int ncount = ;
  59. int i = , j;
  60. j = strlen(p) - ;
  61.  
  62. while (isspace(p[i]) && p[i] != '\0')
  63. {
  64. i++;
  65. }
  66.  
  67. while (isspace(p[j]) && j>)
  68. {
  69. j--;
  70. }
  71.  
  72. ncount = j - i + ;
  73.  
  74. strncpy(p, p + i, ncount);
  75. p[ncount] = '\0';
  76. return ret;
  77. }
  78.  
  79. void main()
  80. {
  81. {
  82. char *p = " abcd ";
  83. char buf[] = { };
  84. trimSpaceStr2(p, buf);
  85. printf("buf = %s\n", buf);
  86. }
  87.  
  88. {
  89. char *p = " abcd ";
  90. trimSpaceStr2_notgood(p);
  91. printf("p = %s\n", p);
  92. }
  93. }

3.字符串反转模型

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. //将某个字符串逆置
  7. void main()
  8. {
  9. char p[] = "abcde";
  10. char c;
  11. char *p1 = p;
  12. char *p2 = p + strlen(p) - ;
  13.  
  14. while (p1 < p2)
  15. {
  16. c = *p1;
  17. *p1 = *p2;
  18. *p2 = c;
  19. ++p1;
  20. --p2;
  21. }
  22.  
  23. printf("p:%s \n", p);
  24. }

4.两个辅助指针变量挖字符串

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. /*
  7. 有一个字符串符合以下特征(”abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";),要求写一个函数(接口),输出以下结果
  8. 1) 以逗号分割字符串,形成二维数组,并把结果传出;
  9. 2) 把二维数组行数运算结果也传出。
  10. */
  11.  
  12. int spitString(const char *buf1, char c, char buf[][], int *num)
  13. {
  14. char *p = NULL;
  15. char *pTmp = NULL;
  16. int ncount = ;
  17. char myBuf[] = { };
  18.  
  19. //步骤1 初始化条件 pTmp,p都执行检索的开头
  20. p = buf1;
  21. pTmp = buf1;
  22. while (*p != '\0')
  23. {
  24. //步骤2 strstr strchr,会让p后移 在p和pTmp之间有一个差值
  25. p = strchr(p, c);
  26. if (p == NULL) //没有找到则跳出来
  27. {
  28. break;
  29. }
  30. else
  31. {
  32. memset(myBuf, , sizeof(myBuf));
  33.  
  34. //挖字符串
  35. strncpy(myBuf, pTmp, p - pTmp);
  36. myBuf[p - pTmp] = '\0';
  37.  
  38. strcpy(buf[ncount], myBuf);
  39.  
  40. ncount++;
  41. //步骤3 让p和pTmp重新初始化,达到检索的条件
  42. pTmp = p = p + ;
  43. }
  44.  
  45. } ;
  46. *num = ncount;
  47. return ;
  48. }
  49.  
  50. int spitString02(const char *buf1, char c, char buf[][], int *num)
  51. {
  52. int ret = ;
  53. char *p = NULL;
  54. char *pTmp = NULL;
  55. int ncount = ;
  56. if (buf1 == NULL || num == NULL)
  57. {
  58. return -;
  59. }
  60. //步骤1 初始化条件 pTmp,p都执行检索的开头
  61. p = buf1;
  62. pTmp = buf1;
  63. while (*p != '\0')
  64. {
  65. //步骤2 strstr strchr,会让p后移 在p和pTmp之间有一个差值
  66. p = strchr(p, c);
  67. if (p == NULL) //没有找到则跳出来
  68. {
  69. break;
  70. }
  71. else
  72. {
  73.  
  74. //挖字符串
  75. strncpy(buf[ncount], pTmp, p - pTmp);
  76. buf[ncount][p - pTmp] = '\0';
  77.  
  78. ncount++;
  79.  
  80. //步骤3 让p和pTmp重新初始化,达到检索的条件
  81. pTmp = p = p + ;
  82. }
  83.  
  84. } ;
  85. *num = ncount;
  86. return ret;
  87. }
  88.  
  89. void main()
  90. {
  91. int ret = , i = ;
  92. const char *buf1 = "abcdef,acccd,";
  93. char c = ',';
  94. char buf[][];
  95. int num = ;
  96. ret = spitString02(buf1, c, buf, &num);
  97. if (ret != )
  98. {
  99. printf("func spitString() err:%d\n", ret);
  100. return ret;
  101. }
  102.  
  103. for (i = ; i<num; i++)
  104. {
  105. printf("%s\n", buf[i]);
  106. }
  107.  
  108. system("pause");
  109. }

C语言进阶之路(二)----字符串操作常见模型的更多相关文章

  1. GO语言的进阶之路-Golang字符串处理以及文件操作

    GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...

  2. 【php学习之路】字符串操作

           无论学习那种语言,字符串操作都是必备的基础.学php的时候总是会不知不觉的与C#比较,用起来总觉得怪怪的没有那么顺手,有些命名也差别很大,再加上很多函数命名是简写形式不百度下还真不知道什 ...

  3. 小白的Python之路 day2 字符串操作 , 字典操作

    1. 字符串操作 特性:不可修改 name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '- ...

  4. Python学习之路3 - 字符串操作&字典

    本节内容: 常用的字符串处理. 格式化输出字符串. 字符串的替换. 字符串和二进制的相互转化. 字典的操作 字符串操作 常用的字符串处理 name = 'vector' print(name.capi ...

  5. Go语言核心36讲(Go语言进阶技术十二)--学习笔记

    18 | if语句.for语句和switch语句 现在,让我们暂时走下神坛,回归民间.我今天要讲的if语句.for语句和switch语句都属于 Go 语言的基本流程控制语句.它们的语法看起来很朴素,但 ...

  6. 在R语言中使用Stringr进行字符串操作

    今天来学习下R中字符串处理操作,主要是stringr包中的字符串处理函数的用法. 先导入stringr包,library(stringr),require(stringr),或者stringr::函数 ...

  7. 苹果新的编程语言 Swift 语言进阶(十二)--选项链

    选项链是使用选项来查询和调用其属性.方法或下标的一个过程,假设选项包括一个值,则属性.方法.下标的查询和调用成功,否则,调用返回nil. 选项链能用在不论什么类型的选项来检查对其一个属性.方法.下标的 ...

  8. C语言进阶之路(一)----C语言的内存四区模型

    内存四区模型:操作系统给C/C++编写的程序分配内存,通常将分配的内存划分为以下四个区域:1.栈区:存放局部变量,用完由操作系统自动释放2.堆区:动态分配给程序的内存区域,由程序员手动释放3.数据区: ...

  9. go语言学习之路 二:变量

    说道变量,首先应该提一提关键字,因为不能把关键字当做变量来声明. 关键字: 下面列出GO语言的关键字或保留字: break default func interface select case def ...

随机推荐

  1. Tensorflow学习资源

    https://tensorflow.google.cn/ 中文官网 https://www.w3cschool.cn/tensorflow_python/tensorflow_python-gnwm ...

  2. COUNT分组条件去重的sql统计语句示例(mysql)

    常规情况下的sql分组统计为: ) from 表 where 条件 group by 字段; 但是有时往往需要添加不同的条件已经去重的统计以上语句就不能满足需求. 解决方案为: 1.添加条件的统计方案 ...

  3. 又一次认识java(七) ---- final keyword

    你总以为你会了,事实上你仅仅是一知半解. final 关键字概览 final关键字可用于声明属性.方法.參数和类,分别表示属性不可变.方法不可覆盖.參数不可变和类不能够继承. 我们来分别看看它的使用方 ...

  4. 使用cnpm 安装vue.js

    前提已经安装了node.js 一.临时使用 1.npm install -g cnpm --registry=https://registry.npm.taobao.org 2.cnpm instal ...

  5. linux下用php将doc、ppt转图片

    解决方案分成两步: (1)调用unoconv命令将 doc.ppt 转 pdf (2)使用 imagemagick将 pdf 转图片 步骤 1.安装unoconv sudo apt-get insta ...

  6. GoLang之错误处理

    错误处理 error Go语言引入了一个错误处理的标准模式,即error接口,该接口定义如下: type error interface { Error() string } 对于大多数函数,如果要返 ...

  7. js复制文本内容到剪贴板

    记录一下使用clipboardData复制不成功. 1.定义一个按钮执行复制 <div> <button type="button" id="copyR ...

  8. modified: xxx(modified content, untracked content)

    当运行git status的时候提示如下: modified: xxx(modified content, untracked content) 我们会很本能的直接执行 add .commit .pu ...

  9. VS每次F5都重新编译代码,即使没有任何修改

    遇到一个奇怪现象,不知道怎么设置了,突然工程的Release模式下,F5每次都要重新编译代码,而且是全部代码都重新编译 而Debug模式没问题 重启VS,重启机器,清理工程重新生成工程都无法解决 最后 ...

  10. docker应用-6(mysql+mycat 搭建数据库集群)

    上一节,通过使用overlay网络,搭建了跨主机的docker容器集群.下面,在这个跨主机的docker容器集群环境下,搭建mysql 数据库集群. mysql主从自动备份和自动切换 从数据安全性考虑 ...