题解 | #统计字符串中各类型字符的个数#

统计字符串中各类型字符的个数

https://www.nowcoder.com/practice/d5b44c494ed24d8ebb10607c469280e3

#include <iostream>
#include <map>
using namespace std;

int main() {
    char str[100] = { 0 };
    cin.getline(str, size(str));
    map<string, int> maps;
    maps.insert(pair<string, int>("letter", 0));
    maps.insert(pair<string, int>("digit", 0));
    maps.insert(pair<string, int>("space", 0));
    maps.insert(pair<string, int>("other", 0));
    for(int i = 0; str[i] != '\0'; i++)
    {
        if(isalpha(str[i])) maps["letter"]++;
        else if(isdigit(str[i])) maps["digit"]++;
        else if(isspace(str[i])) maps["space"]++;
        else maps["other"]++;
    }

    cout << "letter:" << maps["letter"] <<" digit:" << maps["digit"] << " space:" << maps["space"] << " other:" << maps["other"];
}

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
08-09 12:05
美团笔试可以用AI辅助答题,我就直接把其他题也都一起AI了,第一次这么爽的做笔试
喜欢吃卤蛋的肖恩在参...:又疯一个
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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