题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
['a', 'z'] || ['A', 'Z'] 这是字母计数['0', '9'] 这是数字字符,一定要注意不是 [0,9], 数字和数字字符是不一样的[ ] 空格字符,上边三种以外就是其他字符了
#include <iostream> #include <string> using namespace std; int main() { int c1=0,c2=0,c3=0,c4=0; string arr; getline(cin,arr); for(auto ch:arr ) { if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) { c1++; } else if(ch==' ') { c2++; } else if(ch>='0'&&ch<='9') { c3++; } else { c4++; } } cout<<c1<<endl<<c2<<endl<<c3<<endl<<c4<<endl; return 0; }