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

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

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

全部评论

相关推荐

拓竹投了十多天了&nbsp;一直没反应想起来还投了这家&nbsp;看了眼发现已经挂了&nbsp;苦笑(是我太不上心了)
机械打工仔:简历挂和你不太上心有啥关系,别啥都怪自己,自信点
投递拓竹科技等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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