1、提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数(要求使用C++风格字符串完成)
#include <iostream>
using namespace std;
int main()
{
string str;
cout << "please enter str:" << endl;
getline(cin,str);
int capital=0,lowercase=0,num=0,space=0,other=0;
for(size_t i=0;i<str.size();i++)
{
if('A'<=str[i] && 'Z'>=str[i])
{
capital+=1;
}
else if('a'<=str[i] && 'z'>=str[i])
{
lowercase+=1;
}
else if('0'<=str[i] && '9'>=str[i])
{
num+=1;
}
else if(str[i] == ' ')
{
space+=1;
}
else
{
other+=1;
}
}
cout << "capital= " << capital << endl;
cout << "lowercase= " << lowercase << endl;
cout << "num= " << num << endl;
cout << "space= " << space << endl;
cout << "other= " << other << endl;
return 0;
}
2、思维导图