scanf怎么能得到一个句子?

ajiefudan 2002-05-20 03:18:52

char buff[100]
scanf("%s",buff);
printf("your input = %s\n",buff);


当我输入
hello world
的时候
打印为
your input = hello
your input = world
怎么样才能避免空格?
...全文
62 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
superzjx2000 2002-05-20
  • 打赏
  • 举报
回复
getline()
coyj 2002-05-20
  • 打赏
  • 举报
回复
gets(char *string);函数有漏洞,建议用fgets
sbg 2002-05-20
  • 打赏
  • 举报
回复
在scanf中空格作为字符串的结束符号,所以不能用scanf来处理这种问题,
同意楼上的用输入输出字符串的函数!因为scanf("%s"....)遇到空格,回车,换行,制表符及认为输入结束.
用gets(char *string);即可!

blactte 2002-05-20
  • 打赏
  • 举报
回复
因为scanf("%s"....)遇到空格,回车,换行,制表符及认为输入结束.
用gets(char *string);即可!
cornflower 2002-05-20
  • 打赏
  • 举报
回复
using namespace std;
char p[50];
// remove all the white spaces
cin >> ws;
// read characters from stdin until a newline

// or 49 characters have been read
cin.getline(p,50);
// output the result to stdout
cout << p;
sandwish2000 2002-05-20
  • 打赏
  • 举报
回复
在scanf中空格作为字符串的结束符号,所以不能用scanf来处理这种问题,
同意楼上的用输入输出字符串的函数!
coyj 2002-05-20
  • 打赏
  • 举报
回复
scanf("%s",buff);----〉fgets(buff,100,stdin);

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧