鱼C论坛

 找回密码
 立即注册
查看: 1530|回复: 4

[已解决]s1e22动动手strcat

[复制链接]
发表于 2021-9-4 10:50:16 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>

  2. #define max 1024

  3. int main()
  4. {
  5.         char a[max];//第一个字符串
  6.         char b[max];//第二个字符串
  7.         char c[max];//拼接后的字符串
  8.         char *target1 = a;
  9.         char *target2 = b;
  10.         char *target3 = c;
  11.         int i = 0;//统计第一个字符串的字符个数
  12.        
  13.         printf("请输入第一个字符串:");
  14.         fgets(a, max, stdin);
  15.         printf("请输入第二个字符串:");
  16.         fgets(b, max, stdin);

  17.         while((*target3++ = *target1++) != '\0')
  18.         {
  19.                 i++;
  20.         //        printf("连接后的结果是:%d\n", i);               
  21.         }  
  22.         i--;       
  23.         *target3 -= 2 ;
  24.         while((*target3++ = *target2++) != '\0')
  25.         {
  26.                 i++;
  27.         //        printf("连接后的结果是:%d\n", i);
  28.         }
  29.         i--;
  30.        
  31.         printf("连接后的结果是:%s", c);
  32.        
  33.         return 0;
  34. }
复制代码

这是我自己写的代码,但是不知道为什么输出是这样的
我试过了在第二个循环里输出i,i也是有自增的,但是为什么没有第二个字符串没有被复制到第一个字符串后呢??
最佳答案
2021-9-4 10:59:49
完全不需要变量 i
  1. #include <stdio.h>

  2. #define max 1024

  3. int main() {
  4.     char a[max]; //第一个字符串
  5.     char b[max]; //第二个字符串
  6.     char c[max]; //拼接后的字符串
  7.     char *target1 = a;
  8.     char *target2 = b;
  9.     char *target3 = c;

  10.     printf("请输入第一个字符串:");
  11.     fgets(a, max, stdin);
  12.     printf("请输入第二个字符串:");
  13.     fgets(b, max, stdin);

  14.     while((*target3++ = *target1++) != '\0');
  15.     target3 -= 2;
  16.     while((*target3++ = *target2++) != '\0');
  17.     target3 -= 2;
  18.     *target3 = '\0';

  19.     printf("连接后的结果是:%s\n", c);

  20.     return 0;
  21. }
复制代码
1630723682(1).png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-4 10:58:24 | 显示全部楼层
  1. #include <stdio.h>

  2. #define max 1024

  3. int main() {
  4.     char a[max]; //第一个字符串
  5.     char b[max]; //第二个字符串
  6.     char c[max]; //拼接后的字符串
  7.     char *target1 = a;
  8.     char *target2 = b;
  9.     char *target3 = c;
  10.     int i = 0; //统计第一个字符串的字符个数

  11.     printf("请输入第一个字符串:");
  12.     fgets(a, max, stdin);
  13.     printf("请输入第二个字符串:");
  14.     fgets(b, max, stdin);

  15.     while((*target3++ = *target1++) != '\0') {
  16.         i++;
  17.         //        printf("连接后的结果是:%d\n", i);
  18.     }
  19.     i--;
  20.     //*target3 -= 2;
  21.     target3 -= 2;
  22.     while((*target3++ = *target2++) != '\0') {
  23.         i++;
  24.         //        printf("连接后的结果是:%d\n", i);
  25.     }
  26.     i--;
  27.     target3 -= 2;
  28.     *target3 = '\0';

  29.     //printf("连接后的结果是:%s", c);
  30.     printf("连接后的结果是:%s\n", c);

  31.     return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-4 10:59:49 | 显示全部楼层    本楼为最佳答案   
完全不需要变量 i
  1. #include <stdio.h>

  2. #define max 1024

  3. int main() {
  4.     char a[max]; //第一个字符串
  5.     char b[max]; //第二个字符串
  6.     char c[max]; //拼接后的字符串
  7.     char *target1 = a;
  8.     char *target2 = b;
  9.     char *target3 = c;

  10.     printf("请输入第一个字符串:");
  11.     fgets(a, max, stdin);
  12.     printf("请输入第二个字符串:");
  13.     fgets(b, max, stdin);

  14.     while((*target3++ = *target1++) != '\0');
  15.     target3 -= 2;
  16.     while((*target3++ = *target2++) != '\0');
  17.     target3 -= 2;
  18.     *target3 = '\0';

  19.     printf("连接后的结果是:%s\n", c);

  20.     return 0;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-4 11:02:06 | 显示全部楼层
人造人 发表于 2021-9-4 10:59
完全不需要变量 i

我知道在这题里面不需要i,我在前面测试的时候,老是不对,所有加了个i看看是哪里写错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-4 11:37:09 | 显示全部楼层
        错误在这一句
  1.         *target3 -= 2 ;
复制代码

        应该是这样
  1.         target3 -= 2 ;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 14:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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