C语言:统计字符个数及种类

#include <stdio.h>
int main(){
    char c;  //用户输入的字符 
    int shu=0;//字符总数 
    int letters=0,  // 字母数目 
        space=0,  // 空格数目 
        digit=0,  // 整数数目 
        others=0;  // 其他字符数目
    printf("输入一些字符:");
    while((c=getchar())!='\n'){  // 每次读取一个字符,回车时结束
        if(c>='a'&&c<='z'||c>='A'&&c<='Z')
            letters++;
        else if(c==' ')
            space++;
        else if(c>='0'&&c<='9')
            digit++;
        else
            others++;
        shu++; 
    }
    
    printf("\n统计结果:\n字符总数=%d\n英文字母=%d\n空格=%d\n整数=%d\n其他字符=%d\n\n", shu,letters, space, digit, others);
    return 0;
}

 

posted @ 2021-06-14 09:35  myrj  阅读(2261)  评论(0编辑  收藏  举报