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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM