提示并輸入一個(gè)字符串,統(tǒng)計(jì)該字符串中字母、數(shù)字、空格、其他字符的個(gè)數(shù)并輸出
代碼展示
#include <iostream>using namespace std;int main() {string str;int countc = 0; int countn = 0; int count = 0; int counto = 0; cout << "請(qǐng)輸出一個(gè)字符串:" << 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 << "數(shù)字:" << countn << endl;cout << "空格:" << count << endl;cout << "其他字符:" << counto << endl;return 0;
}
運(yùn)行結(jié)果

思維導(dǎo)圖
