提示并输入一个字符串,统计该字符串中字母个数、数字个数、空格个数、其他字符的个数
cpp
#include <iostream>
using namespace std;
int main()
{
cout<<"请输入字符串:";
string str;
getline(cin,str);
int num=0;
int alp=0;
int spa=0;
int other=0;
int len=str.length();
for(int i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
num++;
}else if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z'))
{
alp++;
}else if(str[i]==' ')
{
spa++;
}else
{
other++;
}
}
cout<<"数字"<<num<<"个"<<endl;
cout<<"字母"<<alp<<"个"<<endl;
cout<<"空格"<<spa<<"个"<<endl;
cout<<"其他字符"<<other<<"个"<<endl;
return 0;
}