10.7 作业

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

using namespace std;

int main()
{
    cout << "please enter string: ";
    string str;
    getline(cin, str);
    int count_num = 0, count_A = 0, count_a = 0, count_ = 0, count_else = 0;
    for (int i = 0; i < str.size(); i++)
    {
        if (str.at(i) >= '0' && str.at(i) <= '9')
            count_num++;
        else if (str.at(i) >= 'a' && str.at(i) <= 'z')
            count_a++;
        else if (str.at(i) >= 'A' && str.at(i))
            count_A++;
        else if (str.at(i) == ' ')
            count_++;
        else
            count_else++;
    }
    cout << "大写字母有:" << count_A << " 个" << endl;
    cout << "小写字母有:" << count_a << " 个" << endl;
    cout << "数字有:" << count_num << " 个" << endl;
    cout << "空格有:" << count_ << " 个" << endl;
    cout << "其它字符有:" << count_else << " 个" << endl;
    return 0;
}

你可能感兴趣的:(c++,算法,数据结构)