C++ 提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数

#include 

using namespace std;

int main()
{
    string str;
    cout << "请输入一个字符串>>>" << endl;
    getline(cin, str);
    int size = str.size();
    int xiaoxie =0;
    int daxie =0;
    int shuzi =0;
    int kongge =0;
    int qita =0;
    for(int i=0;i < size;i++)
    {
        if(str.at(i) <= 'z' && str.at(i) >= 'a')
        {
            xiaoxie++;
        }
        else if(str.at(i) <= 'Z' && str.at(i) >= 'A')
        {
            daxie++;
        }
        else if(str.at(i) <= '9' && str.at(i) >= '0')
        {
            shuzi++;
        }
        else if(str.at(i) == ' ')
        {
            kongge++;
        }
        else
        {
            qita++;
        }
    }
    cout << "该字符中大写字母个数: " << daxie << endl;
    cout << "该字符中小写字母个数: " << xiaoxie << endl;
    cout << "该字符中数字个数: " << shuzi << endl;
    cout << "该字符中空格个数: " << kongge << endl;
    cout << "该字符中其他字符个数: " << qita << endl;
    return 0;
}

你可能感兴趣的:(c++,java,算法)