题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
#include <iostream>
using namespace std;
#include <cctype>
#include <string>
int main() {
string str;
getline(cin,str);
int en = 0;
int space = 0;
int num = 0;
int others = 0;
for(char i : str){
if(isalpha(i)){
en++;
}
else if(i == ' '){
space++;
}
else if(isdigit(i)){
num++;
}
else{
others++;
}
}
cout << en << endl;
cout << space << endl;
cout << num << endl;
cout << others << endl;
}
可以用cctype库,如果不用,那用ASCII码来判断也是完全没问题的
华为机试刷题记录 文章被收录于专栏
记录一下手打代码的解题思路方便复习

