作业1:编程
要求:提示并输入一个字符串,统计该字符中大写、小写字母个数、数字个数、空格个数以及其他字符个数
代码:
#include <iostream>using namespace std;int main()
{string str;cout << "请输入一个字符串:" << endl;getline(cin,str);int Bcharcount = 0,Lcharcount = 0,Spacecount = 0,numcount = 0,othercount = 0;for(int i = 0; i < (int)str.size() ;i++){if( str.at(i) >= 'a' && str.at(i) <= 'z' ){Lcharcount++;}else if( str.at(i) >= 'A' && str.at(i) <= 'Z' ){Bcharcount++;}else if( str.at(i) >= '0' && str.at(i) <= '9'){numcount++;}else if( str.at(i) == ' ' ){Spacecount++;}else{othercount++;}}cout << "大写字母个数:" << Bcharcount <<endl;cout << "小写字母个数:" << Lcharcount <<endl;cout << "空格个数:" << Spacecount <<endl;cout << "数字个数:" << numcount <<endl;cout << "其他字符个数:" << othercount <<endl;return 0;
}
效果图: