题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

#include <stdio.h>

int main() {
    char c;
    int enCount=0;
    int spCount=0;
    int nuCount=0;
    int otCount=0;
    while(scanf("%c",&c)&&c!='\n'){
        if(c>='a'&&c<='z'||c>='A'&&c<='Z'){
            enCount++;
        }
        else if(c>='0'&&c<='9'){
            nuCount++;
        }
        else if(c==' '){
            spCount++;
        }
        else{
            otCount++;
        }
    }
    printf("%d\n%d\n%d\n%d\n",enCount,spCount,nuCount,otCount);
    return 0;
}

逐个获取字符,分别根据ASCII统计,最后输出即可。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务