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

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

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

全部评论

相关推荐

10-16 19:36
已编辑
浙江外国语学院 Java
程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
1 1 评论
分享
牛客网
牛客企业服务