C++ day 1

思维导图

C++ day 1_第1张图片

使用C++编写一个程序,输入一个字符串,统计大小写字母、数字、空格和其他符号的个数

#include 

using namespace std;

int main()
{
    int capital = 0;
    int lower = 0;
    int digit = 0;
    int spaces = 0;
    int others = 0;
    cout << "请输入一个字符串:" << endl;
    string str;
    getline(cin,str);
    for(int i = 0;i < str.size();i++)
    {
        if(str.at(i) >= 'A' && str.at(i) <= 'Z')
        {
            capital++;
        }else if(str.at(i) >= 'a' && str.at(i) <= 'z')
        {
            lower++;
        }else if(str.at(i) >= '0' && str.at(i) <= '9')
        {
            digit++;
        }else if(str.at(i) == ' ')
        {
            spaces++;
        }else
        {
            others++;
        }


    }

    cout << "大写字母个数为:" << capital << endl;
    cout << "小写字母个数为:" << lower << endl;
    cout << "数字个数为:" << digit << endl;
    cout << "空格个数为:" << spaces << endl;
    cout << "其他字符个数为:" << others << endl;
    return 0;
}

C++ day 1_第2张图片

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