C 将字符串中的每个字母(按顺序)合并为一个新字符串

C 将字符串中的每个字母(按顺序)合并为一个新字符串,c,C,所以,我对C编程还是很陌生。 有3个长度相同的字符串。 str1=“abc”,str2=“def”,str3=“ghi”。 新字符串中的输出类似于“adgbehcfi” #包括 内部主(空) { 字符str1[32],str2[32],str3[32],s[32]; 整数计数,i,j=0; printf(“插入字符串1:”); scanf(“%s”,str1); printf(“插入字符串2:”); scanf(“%s”,str2); printf(“插入字符串3:”); scanf(“%s”,

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

#包括
内部主(空)
{
字符str1[32],str2[32],str3[32],s[32];
整数计数,i,j=0;
printf(“插入字符串1:”);
scanf(“%s”,str1);
printf(“插入字符串2:”);
scanf(“%s”,str2);
printf(“插入字符串3:”);
scanf(“%s”,str3);
for(count=0;str1[count]!='\0';count++);

(i=0;i
for(i=0,j=0;j
for(i=0,j=0;j)来澄清你的问题

str1[i]=s[j];
意味着

'从s复制第j个字符,并将其放入str1的第i个字符'

你是说反过来

s[i]=str1[j];

来澄清你的问题

str1[i]=s[j];
意味着

'从s复制第j个字符,并将其放入str1的第i个字符'

你是说反过来

s[i]=str1[j];

您确实应该检查目标字符串的大小。这种方法使用指针,操作更少,您可能会喜欢比较

#include<stdio.h>

char*
strmerge3(char* s, char*str1, char*str2, char*str3)
//strmerge3(char* s, long slen, char*str1, char*str2, char*str3)
{
    if(!s) return(s); //handle NULL in string
    char *dp=s; //destination pointer
    char *p1=str1?str1:""; //source pointers
    char *p2=str2?str2:"";
    char *p3=str3?str3:"";
    //long len=0; //slen--; //save room for null terminator

    for( ; (*p1 | *p2 | *p3) ; ) {
        if(*p1) dp++ = p1++; //if(++len>len) break;
        if(*p2) dp++ = p2++; //if(++len>len) break;
        if(*p3) dp++ = p3++; //if(++len>len) break;
    }
    dp = '\0'; //null terminate dest, at end
    return(s);
}

int main(void)
{
    char str1[32], str2[32], str3[32], s[32*3];
    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);

    strmerge3(s,str1,str2,str3);
    //strmerge3(s,sizeof(s),str1,str2,str3); //you should pass s size

    printf("String:%s", s);
    return 0;
}
#包括
煤焦*
strmerge3(char*s,char*str1,char*str2,char*str3)
//strmerge3(char*s,long slen,char*str1,char*str2,char*str3)
{
如果(!s)返回;//字符串中的句柄为NULL
char*dp=s;//目标指针
char*p1=str1?str1:;//源指针
char*p2=str2?str2:“”;
char*p3=str3?str3:;
//long len=0;//slen-->//为空终止符保留空间
对于(;(*p1 |*p2 |*p3);){
if(*p1)dp++=p1++;//if(++len>len)中断;
if(*p2)dp++=p2++;//if(++len>len)中断;
if(*p3)dp++=p3++;//if(++len>len)中断;
}
dp='\0';//null终止dest,在末尾
申报表;
}
内部主(空)
{
字符str1[32],str2[32],str3[32],s[32*3];
整数计数,i,j=0;
printf(“插入字符串1:”);
scanf(“%s”,str1);
printf(“插入字符串2:”);
scanf(“%s”,str2);
printf(“插入字符串3:”);
scanf(“%s”,str3);
strmerge3(s,str1,str2,str3);
//strmerge3(s,sizeof(s),str1,str2,str3);//您应该传递s size
printf(“字符串:%s”,s);
返回0;
}

您确实应该检查目标字符串的大小。这种方法使用指针,操作更少,您可能会喜欢比较

#include<stdio.h>

char*
strmerge3(char* s, char*str1, char*str2, char*str3)
//strmerge3(char* s, long slen, char*str1, char*str2, char*str3)
{
    if(!s) return(s); //handle NULL in string
    char *dp=s; //destination pointer
    char *p1=str1?str1:""; //source pointers
    char *p2=str2?str2:"";
    char *p3=str3?str3:"";
    //long len=0; //slen--; //save room for null terminator

    for( ; (*p1 | *p2 | *p3) ; ) {
        if(*p1) dp++ = p1++; //if(++len>len) break;
        if(*p2) dp++ = p2++; //if(++len>len) break;
        if(*p3) dp++ = p3++; //if(++len>len) break;
    }
    dp = '\0'; //null terminate dest, at end
    return(s);
}

int main(void)
{
    char str1[32], str2[32], str3[32], s[32*3];
    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);

    strmerge3(s,str1,str2,str3);
    //strmerge3(s,sizeof(s),str1,str2,str3); //you should pass s size

    printf("String:%s", s);
    return 0;
}
#包括
煤焦*
strmerge3(char*s,char*str1,char*str2,char*str3)
//strmerge3(char*s,long slen,char*str1,char*str2,char*str3)
{
如果(!s)返回;//字符串中的句柄为NULL
char*dp=s;//目标指针
char*p1=str1?str1:;//源指针
char*p2=str2?str2:“”;
char*p3=str3?str3:;
//long len=0;//slen-->//为空终止符保留空间
对于(;(*p1 |*p2 |*p3);){
if(*p1)dp++=p1++;//if(++len>len)中断;
if(*p2)dp++=p2++;//if(++len>len)中断;
if(*p3)dp++=p3++;//if(++len>len)中断;
}
dp='\0';//null终止dest,在末尾
申报表;
}
内部主(空)
{
字符str1[32],str2[32],str3[32],s[32*3];
整数计数,i,j=0;
printf(“插入字符串1:”);
scanf(“%s”,str1);
printf(“插入字符串2:”);
scanf(“%s”,str2);
printf(“插入字符串3:”);
scanf(“%s”,str3);
strmerge3(s,str1,str2,str3);
//strmerge3(s,sizeof(s),str1,str2,str3);//您应该传递s size
printf(“字符串:%s”,s);
返回0;
}

代码不会将任何内容复制到
s
中。相反,它会将
s
中的随机内容复制到其他字符串中。代码不会将任何内容复制到
s
中。相反,它会将
s
中的随机内容复制到其他字符串中。感谢您的帮助。当我尝试运行程序时,它只显示“adg”@AlexanderAubert查看更新,而不是
i
它应该是
j
。感谢您的帮助。当我尝试运行程序时,它只显示“adg”@AlexanderAubert查看更新,而不是
i
它应该是
j