题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
#include <iostream> #include <string> using namespace std; int main() { string str; getline(cin,str); int word = 0, space = 0, number = 0, other = 0; for(int i = 0; i < str.size(); ++i){ if(str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z') word++; else if(str[i] == ' ') space++; else if(str[i]>='0'&&str[i]<='9') number++; else other++; } cout << word << endl << space << endl << number << endl << other << endl; return 0; }