提示并输入一个字符串,统计该字符串中字母、数字、空格、其他字符的个数并输出
代码展示
#include <iostream>using namespace std;int main() {string str;int countc = 0; int countn = 0; int count = 0; int counto = 0; cout << "请输出一个字符串:" << endl;getline(cin, str);int len = str.size();for (int i = 0; i < len; i++) {if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {countc++;} else if (str[i] >= '0' && str[i] <= '9') { countn++;} else if (str[i] == ' ') {count++;} else {counto++;}}cout << "字母:" << countc << endl;cout << "数字:" << countn << endl;cout << "空格:" << count << endl;cout << "其他字符:" << counto << endl;return 0;
}
运行结果

思维导图
