输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
样例输入
a 1,
样例输出
1
1
1
1
鉴于直接用cin会导致空格无法录入,于是用string的getline函数。
再在对应区间的计数器+1就好
- #include
- #include
- using namespace std;
-
-
-
- int main() {
- int word=0, space=0, num=0,otr = 0;
- string str1;
- getline(cin, str1);
- for (int i = 0; i < str1.length(); i++) {
- if ((str1[i] >= 'a' && str1[i] <= 'z' )|| (str1[i] >= 'A' && str1[i] <= 'Z')) {
- ++word;
- }
-
- else if (str1[i] >= '0' && str1[i] <= '9') {
- ++num;
- }
- else if (str1[i] == ' ') {
- ++space;
- }
- else {
- ++otr;
- }
- }
- std::cout << word << endl;
- std::cout << space << endl;
- std::cout << num << endl;
- std::cout<< otr<
-
- return 0;
- }