练习:提示输入字符串,统计字符串中空格,大写字母,小写字母,其他字符的个数
#include <iostream>
using namespace std;
int main()
{
cout << "请输入字符串:";
string str;
getline(cin,str);//输入带空格的字符串
int len = str.size();//计算字符串的大小
int space = 0,low = 0,rests = 0;
int cap = 0;
for(int i = 0;i<len;i++)//遍历字符串内的字符
{
if(str[i] == ' ')//判断空格
{
space++;
}
else if(str[i]>='A'&&str[i]<='Z')//判断大写字母
{
cap++;
}
else if(str[i]>='a'&&str[i]<='z')//判断小写字母
{
low++;
}
else//其他字符
{
rests++;
}
}
//输出个数
cout<<"空格:"<<space<<" 大写字母:"<<cap<<" 小写字母:"<<low<<" 其他:"<<rests<<endl;
return 0;
}
