gpt4 book ai didi

c - 将字符串中的每个字母(有序)合并为一个新字符串

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:41 27 4
gpt4 key购买 nike

所以,我对 C 编程还是很陌生。
有3个长度相同的字符串。
str1="abc", str2="def", str3="ghi".
新字符串中的输出将类似于“adgbehcfi”。

#include<stdio.h>

int main(void)
{
char str1[32], str2[32], str3[32], s[32];
int count, i, j=0;
printf("Insert String 1:");
scanf("%s", str1);
printf("Insert String 2:");
scanf("%s", str2);
printf("Insert String 3:");
scanf("%s", str3);
for(count=0;str1[count]!='\0';count++);
for(i=0;i<count;i++){
str1[i]=s[j];
str2[i]=s[j+1];
str3[i]=s[j+1];
j++;

}
printf("String:%s", s);
return 0;
}

我尝试运行这个程序,但是当我尝试显示新字符串时,它不会出现。非常感谢所有帮助。

最佳答案

for(i=0, j = 0;j<count;i += 3, j++){
s[i] = str1[j];
s[i+1] = str2[j];
s[i+2] = str3[j];
}
s[i] = '\0';
printf("String:%s ", s);

这就是您想要的 :) 作为一个小挑战,尝试弄清楚它是如何工作的,它将帮助您了解正在发生的事情。对于 SO 社区,我将在大约一个小时后回来编辑解释这是做什么的答案。

关于c - 将字符串中的每个字母(有序)合并为一个新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958241/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com