思路
题目要求统计其中英文字符,空格字符,数字字符,其他字符的个数。
那么用1个 string 来存放读取的数据,读一整行。
然后遍历一遍,用库函数统计即可。
代码
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
string str;
getline(cin, str);
int cntspace = 0, cntalpha = 0, cntdigit = 0, cntother = 0;
for (auto ch : str) {
if (isspace(ch)) { // 空格字符
cntspace++;
} else if (isalpha(ch)) { // 英文字符
cntalpha++;
} else if (isdigit(ch)) { // 数字字符
cntdigit++;
} else { // 其他字符
cntother++;
}
}
cout << cntalpha << endl << cntspace << endl << cntdigit << endl << cntother << endl;
return 0;
}

京公网安备 11010502036488号