输入一行字符,分别统计其中字母、空格、数字和其他字符的个数。
1 // 2 // Created by Green on 2021/6/9. 3 // 4 #include <iostream> 5 6 using namespace std; 7 int letters, digit, space, others; 8 9 int main(void) { 10 cout << "please input some characters" << endl; 11 int c; 12 while ((c = getchar()) != ' ') { 13 if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { 14 letters++; 15 } else if (c == 32) { 16 space++; 17 } else if (c >= '0' && c <= '9') { 18 digit++; 19 } else { 20 others++; 21 } 22 } 23 cout << "letters: " << letters << endl 24 << "digit: " << digit << endl 25 << "space: " << space << endl 26 << "others: " << others << endl; 27 return 0; 28 }