关于getchar()得菜鸟问题
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letters++;
else if(c==' ')
space++;
else if(c>='0'&&c<='9')
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
getch();
}
大家可以看到上面关于getchar()得应用 只要不遇到\n(此时可以理解为回车吧)就一直进行while里的语句 但是当我们进行输入得时候 是不是由于getchar();只接收一个字符 这样我们随便一个输入只要不是回车 他就自动进行下面得操作 只是我们看不到,也就是说下面的运算是不需要回车就能激活得?