c++day1

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

要求使用C++风格字符串完成

#include 

using namespace std;

int main()
{
    string str;
    cout << "请输入一个含有大小写字母,空格,特殊字符的字符串:" << endl;
    getline(cin,str);//getline可以输入含有带空格的字符串
    int size=str.size();
    int big=0,small=0,a=0,k=0,s=0;
    for (int i=0;i='A' && str.at(i)<='Z')
        {
            big++;
        }
        else if (str.at(i)>='a' && str.at(i)<='z')
        {
            small++;
        }
        else if (str.at(i)>='0' && str.at(i)<='9')
        {
            a++;
        }
        else if (str.at(i)==' ')
        {
            k++;
        }
        else
        {
            s++;
        }
    }
    cout << "大写字母=" << big << endl;
    cout << "小写字母=" << small << endl;
    cout << "数字=" << a << endl;
    cout << "空格=" << k << endl;
    cout << "特殊字符=" << s<< endl;
    return 0;
}

c++day1_第1张图片

c++day1_第2张图片

你可能感兴趣的:(c++)