这里要注意判定条件:
字母:a~z
数字:0~9
空格:一个空格
#include<stdio.h>
int main()
{
char a ;
int i;
int number = 0 , space = 0 , letter = 0 , other = 0; // 数字 空格 字母 其他
while ((a = getchar()) && a != '\n')
{
if(a >= '0' && a <= '9')
{
number++;
}
else if(a == ' ')
{
space++;
}
else if((a >= 'A' && a <= 'Z') || (a >= 'a' && a <= 'z' ) )
{
letter++;
}
else
{
other++;
}
}
printf("number:%d\n",number);
printf("space:%d\n",space);
printf("letter:%d\n",letter);
printf("other:%d\n",other);
return 0;
}
输出结果测试:
A1 B2 C3?
number:3
space:2
letter:3
other:1