题目:
输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:
getchar函数依次获取字符。
利用while语句,条件为输入的字符不为"n".
⒉.程序源代码
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char x;
int a,b,c,d;
a=b=c=d;
while((x=getchar())!='\n') {
if(x>='a'&&x<='z')
a++;
else if(x>='0'&&x<='9')
b++;
else if(x==' ')
c++;
else
d++;
}
printf("字母为%d\n",a);
printf("数字为%d\n",b);
printf("空格为%d\n",c);
printf("其他字符为%d\n",d);
return 0;
}
运行截图: