作业要求:
代码:
#include <iostream>
using namespace std;
int main()
{
string str;
cout << "请输入一个字符串:" << endl;
getline(cin,str);
int dx=0,xx=0,sz=0,kg=0,qt=0;
int len =str.size() + 1;
for(int i=0;i<len;i++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
dx++;
}
else if(str[i] >= 'a' && str[i] <= 'z')
{
xx++;
}
else if(str[i] >= '0' && str[i] <= '9')
{
sz++;
}
else if(str[i] == ' ')
{
kg++;
}
else
{
qt++;
}
}
cout << "字符串中大写字母个数为: " << dx << endl;
cout << "字符串中小写字母个数为: " << xx << endl;
cout << "字符串中数字个数为: " << sz << endl;
cout << "字符串中空格个数为: " << kg << endl;
cout << "字符串中其他字符个数为: " << qt << endl;
return 0;
}
代码实现效果图:
思维导图:
模拟面试题目: