题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
#include <iostream>
#include <string>
#include <cctype> // 包含字符类型函数的头文件
using namespace std;
int main() {
string s;
getline(cin, s); // 读取一行文本
int alphaCount = 0, spaceCount = 0, digitCount = 0,
otherCount = 0; // 初始化计数器
// 遍历字符串并分类计数
for (char c : s) {
if (isalpha(c)) {
alphaCount++; // 计数字母
} else if (isspace(c)) { // 使用isspace检查空白字符,包括空格、制表符等
spaceCount++; // 计数空格和其它空白字符
} else if (isdigit(c)) {
digitCount++; // 计数数字
} else {
otherCount++; // 计数其他字符
}
}
// 输出结果
cout << alphaCount << endl
<< spaceCount << endl
<< digitCount << endl
<< otherCount << endl;
return 0;
}

传音控股公司福利 330人发布