鱼C论坛

 找回密码
 立即注册
查看: 797|回复: 3

[已解决]小白求助

[复制链接]
发表于 2020-11-10 22:40:29 | 显示全部楼层 |阅读模式

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

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

x
实现strcat的功能,这样为什么不行啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
谢谢大佬们

#include <stdio.h>
#define MAX 1024

int main()
{
    char str1[MAX];
    char str2[MAX];
    char* target1 = str1;
    char* target2 = str2;
    printf("Please input the first string:");
    fgets(str1, MAX, stdin);
    printf("Please input the second string:");
    fgets(str2, MAX, stdin);
    int i = 0;
    while (*target1++ != '0')
    {
        i++;
    }
    char str[2 * MAX];
    char* target3 = str;
    int n;
    for (
         n = 0; n < i; n++)
    {
        *target3++ = *target1++;
    }
    for (;;)
    {
        if(*target2++ != '0'){*(target3 + n-2) = *target2++;}
    }
    printf("the sentence is:%s", str);
    return 0;
}
最佳答案
2020-11-10 22:56:42
本帖最后由 jackz007 于 2020-11-10 22:58 编辑
  1. #include <stdio.h>

  2. #define MAX 1024

  3. int main()
  4. {
  5.     char str1[MAX] , str2[MAX] , str3[MAX * 2]        ;
  6.     int i , j , m                                     ;
  7.     printf("Please input the first string : ")        ;
  8.     fgets(str1 , MAX , stdin)                         ;
  9.     for(m = 0 ; str1[m] ; m ++)                       ;
  10.     str1[m - 1] = '\0'                                ;
  11.     printf("Please input the second string : ")       ;
  12.     fgets(str2 , MAX , stdin)                         ;
  13.     for(m = 0 ; str2[m] ; m ++)                       ;
  14.     str2[m - 1] = '\0'                                ;
  15.     for(i = 0 ; str1[i] ; i ++) str3[i] = str1[i]     ;
  16.     for(j = 0 ; str2[j] ; j ++) str3[i + j] = str2[j] ;
  17.     str3[i + j] = '\0'                                ;
  18.     printf("the sentence is:%s\n" , str3)             ;
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-10 22:56:42 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-11-10 22:58 编辑
  1. #include <stdio.h>

  2. #define MAX 1024

  3. int main()
  4. {
  5.     char str1[MAX] , str2[MAX] , str3[MAX * 2]        ;
  6.     int i , j , m                                     ;
  7.     printf("Please input the first string : ")        ;
  8.     fgets(str1 , MAX , stdin)                         ;
  9.     for(m = 0 ; str1[m] ; m ++)                       ;
  10.     str1[m - 1] = '\0'                                ;
  11.     printf("Please input the second string : ")       ;
  12.     fgets(str2 , MAX , stdin)                         ;
  13.     for(m = 0 ; str2[m] ; m ++)                       ;
  14.     str2[m - 1] = '\0'                                ;
  15.     for(i = 0 ; str1[i] ; i ++) str3[i] = str1[i]     ;
  16.     for(j = 0 ; str2[j] ; j ++) str3[i + j] = str2[j] ;
  17.     str3[i + j] = '\0'                                ;
  18.     printf("the sentence is:%s\n" , str3)             ;
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-11 12:05:43 | 显示全部楼层
题主,已经帮你重写了一下,因为原来的代码有点混乱。
不过有一个问题因为 fget会读入回车键,所以会出现换行,不过已经实现了两个字符串合并了。

运行示例:

                               
登录/注册后可看大图


  1. #include <stdio.h>
  2. #define MAX 1024

  3. int main()
  4. {
  5.     char str1[MAX];
  6.     char str2[MAX];
  7.     char* target1 = str1;
  8.     char* target2 = str2;
  9.     printf("Please input the first string:");
  10.     fgets(str1, MAX, stdin);
  11.     printf("Please input the second string:");
  12.     fgets(str2, MAX, stdin);
  13.    
  14.     char str[2 * MAX];
  15.     char* target3 = str;
  16.    
  17.     while (*target3++=*target1++);
  18.         target3--;        // 回退一个 现在target3 已经到了 第一个字符串结尾的下一个位置了。所以,这里回退到 '0' 位置
  19.         while (*target3++=*target2++);
  20.    
  21.     printf("the sentence is:%s", str);
  22.     return 0;
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-11 21:51:15 | 显示全部楼层
shooan 发表于 2020-11-11 12:05
题主,已经帮你重写了一下,因为原来的代码有点混乱。
不过有一个问题因为 fget会读入回车键,所以会出现 ...

谢啦~一开始我想用getchar把换行读了,后面发现不行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-9 19:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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