day1-C++

时间:2024-03-12 16:43:15

1>提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数要求使用C++风格字符串完成。

代码:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str ;
    int low = 0, up = 0, num = 0, blank = 0, other = 0;
    cout << "Please input a string:" << endl;
    getline(cin,str);
    for (int i = 0; i < str.size() ; i++) {
        if(islower(str[i]))
            low++;
        else if(isupper(str[i]))
            up++;
        else if(str[i] == ' ')
            blank++;
        else if(str[i] >= 48 && str[i] <= 57)
            num++;
        else
            other++;
    }
    cout << "low = " << low << endl << "up = " << up << endl << "num = " << num << endl << "blank = " << blank <<endl << "other = " << other << endl;
    return 0;
}

结果

2>思维导图