请教C语言习题-4

来源:百度知道 编辑:UC知道 时间:2024/06/03 17:51:04
4. 将两个字符串连接起来,不要用strcat函数。
main()
{
char s1[80],s2[40];
int i=0,j=0;
printf("\nInput the first string:");
gets(s1);
printf("\nInput the second string:");
gets(s2);
while (s1[i] !='\0')
【 】;
while (s2[j] !='\0')
s1[i++]=【 】; /* 拼接字符到s1 */
s1[i] ='\0';
printf("\nNew string: %s",【 】);
}
程序运行结果:
Input the first string:China
Input the second string:Beijing
New string: 【 】

main()
{
char s1[80],s2[40];
int i=0,j=0;
printf("\nInput the first string:");
gets(s1);
printf("\nInput the second string:");
gets(s2);
while (s1[i] !='\0')
i++;
while (s2[j] !='\0')
s1[i++]=s2[j++];/* 拼接字符到s1 */
s1[i] ='\0';
printf("\nNew string: %s",s1);
}
程序运行结果:
Input the first string:China
Input the second string:Beijing
New string: ChinaBeijing

void main()
{
char s1[80],s2[40];
int i=0,j=0;
printf("\nInput the first string:");
gets(s1);
printf("\nInput the second string:");
gets(s2);
while (s1[i] !='\0')
i++;

while (s2[j] !='\0')
s1[i++]=s2[j++];

s1[i] ='\0';
printf("\nNew string: %s\n",s1);
}

Input the