提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数
#include <iostream>
#include<string>
using namespace std;int main()
{string s1;int letter=0,digit=0,space=0,other=0;cout<<"请输入一个字符串:";getline(cin,s1);for(int i=0;i<s1.length();i++){char ch=s1[i];if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){letter++;}else if(ch>='0'&&ch<='9'){digit++;}else if(ch==' '){space++;}else{other++;}}cout<<"字符串中字母的个数为:"<<letter<<"数字的个数为:"<<digit<<"空格的个数为:"<<space<<"其他字符的个数为:"<<other<<endl;return 0;
}
思维导图