思维导图:
提示并输入一个字符串,统计字符中大写、小写个数、空格个数以及其他字符个数要求使用C++风格完成。
代码:
#include <iostream>
#include<array>
using namespace std;int main()
{string str;cout << "请输入一个字符串:" ;getline(cin,str);int line=(str.size());//记录有多长int big_num=0;//大写个数int small_num=0;//小写个数int num_num=0; //数字个数int empty_num=0;//空格个数int other_num=0;//其他个数for(int i=0;i<line;i++){if(str[i]>='A'&&str[i]<='Z')//大写字母{big_num++;}else if(str[i]>='a'&&str[i]<='z')//小写字母{small_num++;}else if(str[i]>='0' && str[i]<='9' )//数字个数{num_num++;}else if(str[i]==' ')//空格{empty_num++;}else//其他符号{other_num++;}}cout << "这个字符串总共有" << line << "个字符" << endl;cout << "big_num=" << big_num << endl;cout << "small_num=" << small_num << endl;cout << "num_num=" << num_num << endl;cout << "empty_num=" << empty_num << endl;cout << "other_num=" << other_num << endl;return 0;
}
运行效果: