輸入有空格的字符串有2種方法
1、使用gets函數
Eg:
char s[100];
gets(s);
2、利用scanf的%[]格式控制符
Eg:輸入I love you!
#include "stdio.h"
void main()
{
char str[50];
scanf("%[^\n]",str); /*scanf("%s",str);不能接收空格符*/
printf("%s\n",str);
}
輸入:I□love□you! ↘ (□代表空格,↘代表回車)
輸出:I love you!
這里的scanf("%[^\n]",str);表示輸入的字符串以回車結束。