题解 | #统计字符串中各类型字符的个数#
统计字符串中各类型字符的个数
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"];
}

查看3道真题和解析