我想用getchar统计输入的换行个数,空格个数,以及输入字符的总个数,但却做不到了~~

Turbo_CS 2007-08-10 05:46:03
这个是我的程序,要么就只能统计总个数,要么只能分别统计空格的和换行的,大家看看该怎么解才好呢?

#include<stdio.h>
void main()
{
int c;
int nw=0;

int nk=0;

int nr=0;
while
(

(c= getchar() )!=EOF


)
//nw++; (统计总个数的我不知道该放到哪里好)
if(c=='\n')

nr++;
else if(c==' ')
nk++;





printf("\nthe rows is : %d",nr);
printf("\nthe blank is : %d",nk);
printf("\nthe all chars is : %d",nw);

}
...全文
135 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Turbo_CS 2007-08-10
  • 打赏
  • 举报
回复
不过,hanb99(寒冰)的答案不对哦!
那样程序无法运行
应该这样的~~
while((c = getchar()) != EOF)
{

nw++;

if(c=='\n')
nr++;
else if(c==' ')
nk++;

}
无论如何也要感谢你的提示啊!
jxlczjp77 2007-08-10
  • 打赏
  • 举报
回复
while ( (c= getchar() )!=EOF )
{//加大括号
nw++;
if(c=='\n')
nr++;
else if(c==' ')
nk++;
}
Turbo_CS 2007-08-10
  • 打赏
  • 举报
回复
哈哈,已经知道答案啦!!
原来我少写了括号!
hanb99 2007-08-10
  • 打赏
  • 举报
回复
#include<stdio.h>
void main()
{
int c;
int nw=0;
int nk=0;
int nr=0;
while
{
(c= getchar() )!=EOF
if(c=='\n')
nr++;
else if(c==' ')
nk++;
}
printf("\nthe rows is : %d",nr);
printf("\nthe blank is : %d",nk);
printf("\nthe all chars is : %d",nw);

}

69,373

社区成员

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

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