可以使用C++ string 内置函数实现

#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    for (int i=0;i<=str.length();i++){
        if (isalpha(str[i])){
            chars++;
            continue;
        }
        if (isdigit(str[i])){
            digits++;
            continue;
        }
        if (isspace(str[i])){
            whitespace++;
            continue;
        }
        else{
            others++;
            continue;
        }
    }

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others-1 << endl;

    return 0;
}