C语言输入一行字符,分别统计出其中英文字母、空格、数字与其它字符得个数。
#include<stdio.h>
void main()
{
char c;
int letter = 0, space = 0, digit = 0, other = 0;
printf("请输入需要统计的字段:\n");
while ((c = getchar()) != '\n')//运用getchar逐个识别,回车结束
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letter++;
else if (c == ' ')
space++;
else if (c >= '0' && c <= '9')
digit++;
else
other++;
}
printf("letter=%d\nspace=%d\ndigit=%d\nother=%d", letter, space, digit, other);
}