C++ 程序题:统计字符串中字母、数字、空格和其他字符个数
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // 输入字符串,可以包含空格
int cnt_letter = 0, cnt_digit = 0, cnt_space = 0, cnt_other = 0;
for (int i = 0; i < s.size(); i++) {
if (isalpha(s[i])) cnt_letter++; // 判断字母
else if (isdigit(s[i])) cnt_digit++; // 判断数字
else if (isspace(s[i])) cnt_space++; // 判断空格
else cnt_other++; // 其他字符
}
cout << cnt_letter << ',' << cnt_digit << ',' << cnt_space << ',' << cnt_other << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nKMK 著作权归作者所有。请勿转载和采集!