C语言 字符串分类统计 输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数
题目:
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数
样例输入:
aklsjflj123 sadf918u324 asdf91u32oasdf/.’;123
样例输出:
23 16 2 4
代码如下:
#include <stdio.h>
int main()
{
int letter=0,number=0,blank=0,others=0,c;
while((c=getchar())!='\n')
{
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
letter++;
else if(c>='0'&&c<='9')
number++;
else if(c==' ')
blank++;
else
others++;
}
printf("%d %d %d %d",letter,number,blank,others);
return 0;
}
注意:
- 只需注意需要分类的类别特性,按照各自特性进行选择;
- 用getchar键入字符,便于直接对输入内容进行操作;