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

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

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

全部评论

相关推荐

Rena1ssance_:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
喜欢飞来飞去的雪碧在刷代码:可以试一试字节
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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