如何用scanf()将两个带空格符的字符串合并成一个

main()
{char *p1[20],*p2[20];
printf("please input two strings;");
scanf("%s%s",p1,p2);
copy(p1,p2);
printf("the new strings ;%s",p1);
getch();}
copy (char *p1,char *p2)
{
while (*p1!='\0')
p1++;
while (*p2!='\0')
{*p1=*p2;p1++;p2++;}
*p1='\0';}
这个可以实现两个不带字符串的合并,但是如果我想将HELLO WORLD和I LOVE YOU合并,应该如何做?谢谢各们DX

如果要输入带空格的字符串,不能用scanf(),可以用gets()
一下程序是我自己编的,在turbo c下运行通过。
#include <stdio.h>
#include <string.h>

void main()
{
char p1[20],p2[20];
printf("string 1:");
gets(p1);
printf("\nstring2:");
gets(p2);
strcat(p1,p2);
printf("new string:%s",p1);
getch();
}
输出结果:hello worldi love you
中间没有空格,如果想有空格,可以在第一个字符串输入完毕时在在其后面加一个空格。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-04-10
你把scanf函数换成gets函数
因为gets()函数可以从键盘输入字符串 在遇到回车时才停止输入
而scanf在遇到空格时算前面输入部分为一个字符串,所以不能用来输入带空格的字符串
相似回答