输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。

题目内容: 输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。

例: (1)输入:I love hebeu! 输出:character:10,space:2,digit:0,others:1 (2)输入:2020, have a brilliant year! 输出:character:18,space:4,digit:4,others:2

答案:
#include<stdio.h>
int main()
{
    char c;
    int letters=0,spaces=0,digits=0,others=0;
    while((c=getchar())!='\n')
    {
        if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
        letters++;
        else if(c>='0'&&c<='9')
        digits++;
        else if(c==' ')
        spaces++;
        else
        others++;
    }
    printf("character:%d,space:%d,digit:%d,others:%d",letters,spaces,digits,others);
    return 0;
}                      

评论

暂无评论