#include<stdio.h>
int main()
{
char s1[10], s2[10], s3[10], s4[10], s5[10], s6[10], s7[10], s8[10], s9[10], s10[10];
scanf("%s", &s1);
scanf("%s", &s2);
scanf("%s", &s3);
scanf("%s", &s4);
scanf("%s", &s5);
scanf("%s", &s6);
scanf("%s", &s7);
scanf("%s", &s8);
scanf("%s", &s9);
scanf("%s", &s10);
int i = 0;
int j = 0;
char *str;
char a[100];
str = &s1[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s2[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s3[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s4[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s5[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s6[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s7[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s8[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s9[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
str = &s10[0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
a[i] = '\0';
printf("%s\n", a);
return 0;
}
在windows平台下,通常会这么干:
wsprintf(buffer,"%s%s%s%s%s%s%s%s%s%s",str1,str2,str3,str4,str5,str6,str7,str8,str9,str10);
其他平台的话,我记得string.h里面有个专门拼接字符串的函数,具体名字不记得了,有兴趣的可以查一下。
添加#include<string.h>,使用strcat函数可以拼接两字符串,如:strcat(s1,s2)将s2拼接到s1后面,s1="ab",s2="cd",拼接后,s1=“abcd”
用二维数组,可以减少代码量:
#include<stdio.h>
int main()
{
int i=0,j;
char *str,ss[10][10],a[100];
for(j=0;j<10;j++)
{
scanf("%s", &ss[j]);
}
for(j=0;j<10;j++)
{
str = &ss[j][0];
while(*str != '\0')
{
a[i] = *str;
i++;
str++;
}
}
a[i] = '\0';
printf("%s\n", a);
return 0;
}