VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1340|回复: 1

[交流] C学习之路的点滴记录 基础语法玩转hello 大牛飘过

[复制链接]
13_avatar_middle
最佳答案
1 
在线会员 发表于 2021-4-18 09:50:29 | 显示全部楼层 |阅读模式
本帖最后由 小白学编程 于 2021-4-18 12:43 编辑

从C语言的 printf("hello world")到 puts("hello world"); 到 c++的cout<<"hello world"<<endl;

第一种写法:
  1. char ch1[] = "hello";
  2.         char ch2[] = "world";
  3.         char ch3[20] = { 0 };
  4.         int i = 0;
  5.        
  6.         while (ch1[i]!='\0')
  7.         {
  8.                 ch3[i] = ch1[i];
  9.                 i++;
  10.         }
  11.         int j = 0;
  12.         while (ch2[j]) //等价于ch2[j]!='\0';等价于ch2[j]!=0;
  13.         {
  14.                 ch3[i+j] = ch2[j];
  15.                 j++;
  16.         }
  17.         ch3[i + j] = '\0';
  18.         printf("%s \n", ch3);
  19.         system("pause");
  20.         return EXIT_SUCCESS;
复制代码

第二种写法:
  1. void my_strcpy(char *ch1, char *ch2)
  2. {
  3.         int i = 0;
  4.         while (ch1[i]!='\0')
  5.         {
  6.                 i++;
  7.         }
  8.         int j = 0;
  9.         while (ch2[j]!=0)
  10.         {
  11.                 ch1[i + j] = ch2[j];
  12.                 j++;
  13.         }
  14.         ch1[i + j] = '\0';
  15. }


  16. int main()
  17. {
  18.         char ch1[30] = "hello";
  19.         char ch2[] = "world";
  20.        
  21.         my_strcpy(ch1, ch2);
  22.         printf("%s \n", ch1);
  23.         system("pause");
  24.         return EXIT_SUCCESS;
  25. }
复制代码

第三种写法:
  1. void my_strcpy(char *ch1, char *ch2)
  2. {
  3.         int i = 0;
  4.         while (*(ch1+i)!='\0')
  5.         {
  6.                 i++;
  7.         }
  8.         int j = 0;
  9.         while (*(ch2+j)!='\0')
  10.         {
  11.                 *(ch1+i + j) = *(ch2 + j);
  12.                 j++;
  13.         }
  14. }

  15. int main()
  16. {
  17.         char ch1[30] = "hello";
  18.         char ch2[] = "world";
  19.        
  20.         my_strcpy(ch1, ch2);
  21.         printf("%s \n", ch1);
  22.         system("pause");
  23.         return EXIT_SUCCESS;
  24. }
复制代码

第四种写法:
  1. void my_strcpy(char *ch1, char *ch2)
  2. {
  3.         while (*ch1)ch1++;
  4.         while (*ch2)
  5.         {
  6.                 *ch1 = *ch2;
  7.                 ch1++;
  8.                 ch2++;
  9.         }
  10.        
  11. }

  12. int main()
  13. {
  14.         char ch1[30] = "hello";
  15.         char ch2[] = "world";
  16.        
  17.         my_strcpy(ch1, ch2);
  18.         printf("%s \n", ch1);
  19.         system("pause");
  20.         return EXIT_SUCCESS;
  21. }
复制代码

第五种写法:
void my_strcpy(char *ch1, char *ch2)
{
        while (*ch1)ch1++;
        while (*ch1++ = *ch2++);
}
int main()
{
        char ch1[30] = "hello";
        char ch2[] = "world";
       
        my_strcpy(ch1, ch2);
        printf("%s \n", ch1);
        system("pause");
        return EXIT_SUCCESS;
}
第六种写法 hello去掉空格
  1. void Remove_space(char *ch1)
  2. {
  3.         char str[100] = { 0 };
  4.         int i = 0;
  5.         int j = 0;
  6.         char *temp = str;
  7.         while (ch1[i]!='\0')
  8.         {
  9.                 if (ch1[i] !=' ')
  10.                 {
  11.                         str[j] = ch1[i];
  12.                         j++;
  13.                 }
  14.                 i++;
  15.         }
  16.        
  17.         while (*ch1++ = *temp++);
  18. }
  19. int main()
  20. {
  21.         char ch1[] = " h e l l o w o r l d";
  22.         Remove_space(ch1);
  23.         printf("%s \n", ch1);
  24.         system("pause");
  25.         return EXIT_SUCCESS;
  26. }
复制代码

第七种写法 hello去掉空格
  1. void Remove_space(char *ch1)
  2. {
  3.         //用来遍历字符串
  4.         char *ftemp = ch1;
  5.         //记录非空格字符串
  6.         char *rtemp = ch1;
  7.         while (*ftemp)
  8.         {
  9.                 if (*ftemp!=' ')
  10.                 {
  11.                         *rtemp = *ftemp;
  12.                         rtemp++;
  13.                 }
  14.                 ftemp++;
  15.         }
  16.         *rtemp = 0;
  17. }
  18. int main()
  19. {
  20.         char ch1[] = " h e l l o w o r l d";
  21.         Remove_space(ch1);
  22.         printf("%s \n", ch1);
  23.         system("pause");
  24.         return EXIT_SUCCESS;
  25. }
复制代码

第8种写法
  1. int main()
  2. {
  3.         char ch1[] = "hello world";
  4.         char *p = ch1;
  5.         printf("%s \n", ch1);
  6.         printf("%s \n", p);
  7.         printf("%c \n", *(p+1)); //+1指向e 从0索引  可以加2-3-4-5等
  8.         system("pause");
  9.         return EXIT_SUCCESS;
  10. }
复制代码

第九种写法 strcpy
  1. int main()
  2. {
  3.         char ch1[] = "hello world";
  4.         char str[100];
  5.         strcpy(str, ch1);
  6.         printf("%s \n", ch1);

  7.         system("pause");
  8.         return EXIT_SUCCESS;
  9. }
复制代码

第十种写法
  1. void my_strcpy(char *dest, const char *src)
  2. {
  3.         while (*dest++ = *src++);
  4. }
  5. int main()
  6. {
  7.         char ch1[] = "hello world";
  8.         char str[100];
  9.         //strcpy(str, ch1);
  10.         my_strcpy(str, ch1);
  11.         printf("%s \n", ch1);
  12.         system("pause");
  13.         return EXIT_SUCCESS;
  14. }
复制代码

第十一种:倒序
  1. void inverse(char *ch)
  2. {
  3.         int i = 0;
  4.         int j = strlen(ch) - 1;
  5.         while (i<j)
  6.         {
  7.                 char temp = ch[i];
  8.                 ch[i] = ch[j];
  9.                 ch[j] = temp;
  10.                 i++;
  11.                 j--;
  12.         }
  13. }
  14. int main()
  15. {
  16.         char ch[] = "hello world";
  17.         inverse(ch);
  18.         printf("%s \n", ch);
  19.         system("pause");
  20.         return EXIT_SUCCESS;
  21. }
复制代码

第十二种:
  1. void inverse(char *ch)
  2. {
  3.         char *ftemp = ch;
  4.         char *btemp = ch + strlen(ch) - 1;
  5.         while (ftemp < btemp)
  6.         {
  7.                 char temp = *ftemp;
  8.                 *ftemp = *btemp;
  9.                 *btemp = temp;
  10.                 *ftemp++;
  11.                 *btemp--;
  12.         }
  13. }
  14. int main()
  15. {
  16.         char ch[] = "hello world";
  17.         inverse(ch);
  18.         printf("%s \n", ch);
  19.         system("pause");
  20.         return EXIT_SUCCESS;
  21. }
复制代码

第十三种:
  1. int main()
  2. {
  3.         char dest[] = "hello";
  4.         char src[] = "world";
  5.         strcat(dest, src);
  6.         printf("%s \n", dest);
  7.         system("pause");
  8.         return EXIT_SUCCESS;
  9. }
复制代码

第十四种
  1. void my_strcat(char *dest, const char *src)
  2. {
  3.         while (*dest)*dest++;
  4.         while (*dest++ = *src++);
  5. }
  6. int main()
  7. {
  8.         char dest[] = "hello";
  9.         char src[] = "world";
  10.         my_strcat(dest, src);
  11.         printf("%s \n", dest);
  12.         system("pause");
  13.         return EXIT_SUCCESS;
  14. }
复制代码

第十五种:
  1. int main()
  2. {
  3.         char dest[] = "hello";
  4.         char src[] = "world";
  5.         strncat(dest, src, 20);
  6.         printf("%s \n", dest);
  7.         system("pause");
  8.         return EXIT_SUCCESS;
  9. }
复制代码

第十六种
  1. void my_strncat(char *dest, char *src, size_t n)
  2. {
  3.         while (*dest)*dest++;
  4.         while ((*dest++ = *src++)&&--n);
  5. }
  6. int main()
  7. {
  8.         char dest[] = "hello";
  9.         char src[] = "world";
  10.         my_strncat(dest, src,20);
  11.         printf("%s \n", dest);
  12.         system("pause");
  13.         return EXIT_SUCCESS;
  14. }
复制代码

第十七种  字符比较
  1. int main()
  2. {
  3.         char ch1[] = "hello world1";
  4.         char ch2[] = "hello world";
  5.         int value = strcmp(ch1, ch2);
  6.         printf("%d \n", value);
  7.         system("pause");
  8.         return EXIT_SUCCESS;
  9. }
复制代码

第十八种
  1. int my_strcmp(const char *ch1, char *ch2)
  2. {
  3.         while (*ch1==*ch2)
  4.         {
  5.                 if (*ch1=='\0')
  6.                 {
  7.                         return 0;
  8.                 }
  9.                 ch1++;
  10.                 ch2++;
  11.         }
  12.         return *ch1 > ch2 ? 1 : -1;
  13. }
  14. int main()
  15. {
  16.         char ch1[] = "hello world1";
  17.         char ch2[] = "hello world";
  18.         int value = strcmp(ch1, ch2);
  19.         printf("%d \n", value);
  20.         system("pause");
  21.         return EXIT_SUCCESS;
  22. }
复制代码

第十九种
  1. int my_strcmp(const char *ch1, char *ch2)
  2. {
  3.         while (*ch1++ == *ch2++&&!*ch1);
  4.         if (!*ch1&&!*ch2)return 0;
  5.         return *ch1 > ch2 ? 1 : -1;
  6. }
  7. int main()
  8. {
  9.         char ch1[] = "hello world1";
  10.         char ch2[] = "hello world";
  11.         int value = strcmp(ch1, ch2);
  12.         printf("%d \n", value);
  13.         system("pause");
  14.         return EXIT_SUCCESS;
  15. }
复制代码

第二十种
  1. int my_strcmp(const char *ch1, char *ch2)
  2. {
  3.         while (*ch1++ == *ch2++&&!*ch1);
  4.         if (!*--ch1&&!*--ch2)return 0;
  5.         return *ch1 > ch2 ? 1 : -1;
  6. }
  7. int main()
  8. {
  9.         char ch1[] = "hello world1";
  10.         char ch2[] = "hello world";
  11.         int value = strcmp(ch1, ch2);
  12.         printf("%d \n", value);
  13.         system("pause");
  14.         return EXIT_SUCCESS;
  15. }
复制代码

C学习之路的点滴记录 基础语法玩转hello  大牛飘过

评分

参与人数 1驿站币 +2 热心值 +2 收起 理由
58_avatar_small thzzl + 2 + 2 很给力!

查看全部评分





上一篇:C++生成dll动态链接库
下一篇:关于CPropertySheet使用多字节编码抛出异常的问题
92_avatar_middle
最佳答案
0 
online_vip 发表于 2021-4-20 16:59:41 | 显示全部楼层
牛逼,一顿操作猛如虎
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-4 20:10

Powered by CcTry.CoM

© 2009-2021 cctry.com

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