鱼C论坛

 找回密码
 立即注册
查看: 2457|回复: 0

[学习笔记] 007-字符串的三种典型模型

[复制链接]
发表于 2018-8-8 16:45:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 moc 于 2018-8-8 16:47 编辑

1、字符串的拷贝--推演
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "string.h"

  4. void copy_str2(char *from, char *to)
  5. {
  6.         for (;*from != '\0';to++,from++)
  7.         {
  8.                 *to = *from;
  9.         }
  10.         *to = '\0';
  11. }

  12. // 后缀++的优先级高于*p,所以先执行*p,在执行++,{后缀(先执行在++)}
  13. void copy_str3(char *from, char *to)
  14. {
  15.         while (*from != '\0')
  16.         {
  17.                 // *to = *from;
  18.                 // to++; from++;
  19.                 *to++ = *from++;
  20.         }
  21.         *to = '\0';
  22. }

  23. //上面函数继续优化
  24. void copy_str4(char *from, char *to)
  25. {
  26.         while ((*to++ = *from++) != '\0') ;
  27. }

  28. void main()
  29. {
  30.         char from[20];
  31.         char  to[20];

  32.         strcpy_s(from, "adadada123");
  33.         copy_str4(from, to);
  34.         printf("%s", to);

  35.         system("pause");
  36. }
复制代码

2、两头堵模型
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "string.h"

  4. //两头堵,去除两边的空格,得出中间字符串的长度
  5. int trispace(char * inbuf, char *outbuf)
  6. {
  7.         int count = 0, ret = 0;
  8.         int i = 0, j = 0;

  9.         char *p = inbuf;
  10.         j = strlen(p) - 1;

  11.         //浮动指针查找关键位置
  12.         while (p[i] == ' ' && p[i] != '\0')
  13.         {
  14.                 i++;
  15.         }

  16.         while (p[j] == ' ' && j > 0)
  17.         {
  18.                 j--;
  19.         }

  20.         count = j - i + 1;
  21.         printf("count:%d\n", count);

  22.         memcpy(outbuf, inbuf + i, count);
  23.         outbuf[count] = '\0';

  24.         return ret;
  25. }

  26. void  main()
  27. {
  28.         int ret = 0;
  29.         char bu = NULL;

  30.         //char *p = "     abcdefg     ";
  31.         char buf[] = "     abcdefg     ";
  32.         char buf2[100];

  33.         ret = trispace(buf, buf2);
  34.         if (ret != 0)
  35.         {
  36.                 printf("fun Error: code %d", ret);
  37.         }
  38.         printf("buf2:%s", buf2);

  39.         system("pause");
  40. }
复制代码

3、字符串反转模型
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "string.h"

  4. //求取已知字符串的反序
  5. void main()
  6. {
  7.         char c;
  8.         //char *str;
  9.         char str[] = "abcdefg";
  10.         int len = strlen(str);
  11.         char *p1 = str;
  12.         char *p2 = str + len - 1;

  13.         //利用首尾指针取*不断交换
  14.         while (p1 < p2)
  15.         {
  16.                 c = *p1;
  17.                 *p1 = *p2;
  18.                 *p2 = c;
  19.                 p1++;
  20.                 p2--;
  21.         }
  22.         printf("str:%s\n", str);

  23.         system("pause");
  24. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 23:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表