题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
#include <iostream>
#include<string>
using namespace std;
int main() {
string str;
getline(cin, str);
int countAbc = 0, countKong = 0, countNum = 0, countOther = 0;
for (int i = 0; i < str.size(); i++) {
if ((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122)) { //字符计数
countAbc++;
} else if (str[i] == ' ') { //空格计数
countKong++;
} else if (str[i] >= 48 && str[i] <= 57) { //数字计数
countNum++;
} else { //其他字符计数
countOther++;
}
}
cout << countAbc << endl;
cout << countKong << endl;
cout << countNum << endl;
cout << countOther;
return 0;
}
