题解 | #统计字符#

统计字符

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

#include <ctype.h>
#include <stdio.h>
#include <string.h>

// 1. 得到一行输入字符串
// 2. 遍历字符串,统计对应字符数
// 3. 输出个数

int main() {
    
    char input[1010] = {0};
    gets(input);

    int alp_cnt = 0;
    int space_cnt = 0;
    int num_cnt = 0;
    int other_cnt = 0;

    int len = strlen(input);
    for(int i = 0; i < len; ++ i)
    {
        if(isalpha(input[i]))
        {
            ++ alp_cnt;
        }
        else if(isspace(input[i]))
        {
            ++ space_cnt;
        }
        else if(isdigit(input[i]))
        {
            ++ num_cnt;
        }
        else {
            ++ other_cnt;
        }
    }

    printf("%d\n", alp_cnt);
    printf("%d\n", space_cnt);
    printf("%d\n", num_cnt);
    printf("%d", other_cnt);
    

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:23
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
吴offer选手:HR:我KPI到手了就行,合不合适关我什么事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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