鱼C论坛

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

[已解决]s1e22动动手strcat

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

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

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

x
#include<stdio.h>

#define max 1024

int main()
{
        char a[max];//第一个字符串 
        char b[max];//第二个字符串 
        char c[max];//拼接后的字符串 
        char *target1 = a; 
        char *target2 = b;
        char *target3 = c;
        int i = 0;//统计第一个字符串的字符个数
        
        printf("请输入第一个字符串:");
        fgets(a, max, stdin); 
        printf("请输入第二个字符串:");
        fgets(b, max, stdin); 

        while((*target3++ = *target1++) != '\0')
        {
                i++;
        //        printf("连接后的结果是:%d\n", i);                
        }  
        i--;        
        *target3 -= 2 ;
        while((*target3++ = *target2++) != '\0')
        {
                i++;
        //        printf("连接后的结果是:%d\n", i); 
        } 
        i--;
        
        printf("连接后的结果是:%s", c);
        
        return 0;
 } 
这是我自己写的代码,但是不知道为什么输出是这样的
我试过了在第二个循环里输出i,i也是有自增的,但是为什么没有第二个字符串没有被复制到第一个字符串后呢??
最佳答案
2021-9-4 10:59:49
完全不需要变量 i
#include <stdio.h>

#define max 1024

int main() {
    char a[max]; //第一个字符串
    char b[max]; //第二个字符串
    char c[max]; //拼接后的字符串
    char *target1 = a;
    char *target2 = b;
    char *target3 = c;

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

    while((*target3++ = *target1++) != '\0');
    target3 -= 2;
    while((*target3++ = *target2++) != '\0');
    target3 -= 2;
    *target3 = '\0';

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

    return 0;
}
1630723682(1).png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

#define max 1024

int main() {
    char a[max]; //第一个字符串
    char b[max]; //第二个字符串
    char c[max]; //拼接后的字符串
    char *target1 = a;
    char *target2 = b;
    char *target3 = c;
    int i = 0; //统计第一个字符串的字符个数

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

    while((*target3++ = *target1++) != '\0') {
        i++;
        //        printf("连接后的结果是:%d\n", i);
    }
    i--;
    //*target3 -= 2;
    target3 -= 2;
    while((*target3++ = *target2++) != '\0') {
        i++;
        //        printf("连接后的结果是:%d\n", i);
    }
    i--;
    target3 -= 2;
    *target3 = '\0';

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

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

#define max 1024

int main() {
    char a[max]; //第一个字符串
    char b[max]; //第二个字符串
    char c[max]; //拼接后的字符串
    char *target1 = a;
    char *target2 = b;
    char *target3 = c;

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

    while((*target3++ = *target1++) != '\0');
    target3 -= 2;
    while((*target3++ = *target2++) != '\0');
    target3 -= 2;
    *target3 = '\0';

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

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

我知道在这题里面不需要i,我在前面测试的时候,老是不对,所有加了个i看看是哪里写错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-4 11:37:09 | 显示全部楼层
        错误在这一句
        *target3 -= 2 ;
        应该是这样
        target3 -= 2 ;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-22 04:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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