输入有空格的字符串的2种方法

输入有空格的字符串有2种方法
1、使用gets函数
Eg:
char s[100];
gets(s);

2、利用scanf的%[]格式控制符
Eg:输入I love you!
#include "stdio.h"
void main()
{
char str[50];
scanf("%[^ ]",str);   /*scanf("%s",str);不能接收空格符*/
printf("%s ",str);
}
输入:I□love□you! ↘ (□代表空格,↘代表回车)
输出:I love you!
这里的scanf("%[^ ]",str);表示输入的字符串以回车结束。

原文地址:https://www.cnblogs.com/Theo-sblogs/p/11234821.html