提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数(要求使用C++风格字符串完成)
#include <iostream>
using namespace std;
int main()
{
string str;
cout << "请输入一个字符串:" << endl;
getline(cin, str);
int size = str.size();
int daxie =0;
int xiaoxie =0;
int numb =0;
int space =0;
int other =0;
for(int i=0;i < size;i++)
{
if(str.at(i) <= 'Z' && str.at(i) >= 'A')
{
daxie++;
}
else if(str.at(i) <= 'z' && str.at(i) >= 'a')
{
xiaoxie++;
}
else if(str.at(i) <= '9' && str.at(i) >= '0')
{
numb++;
}
else if(str.at(i) == ' ')
{
space++;
}
else
{
other++;
}
}
cout << "字符串中大写字母个数:" << daxie << endl;
cout << "字符串中小写字母个数:" << xiaoxie << endl;
cout << "字符串中数字个数:" << numb << endl;
cout << "字符串中空格个数:" << space << endl;
cout << "字符串中其他字符个数:" << other << endl;
return 0;
}
效果图: