您的位置:首页 > 其它

【华为OJ平台练习题】统计一段字符串中含有空格、英文、数字的个数

2015-08-12 17:33 429 查看
//统计一段字符串中含有空格、英文、数字的个数

#include <iostream>

using namespace std;

void processString(char* s)
{
int n = strlen(s);
int kg=0;
int shuzi=0;
int yingwen=0;
if(n>0)
{
for(int a=0;a<n;a++)
{
if(s[a]==' ')
kg++;
if(s[a]<='9'&&s[a]>='0')
shuzi++;
if(s[a]<='z'&&s[a]>='a')
yingwen++;
if(s[a]<='Z'&&s[a]>='A')
yingwen++;
}
cout<<s<<"含有空格个数为:"<<kg<<endl;
cout<<s<<"含有数字个数为:"<<shuzi<<endl;
cout<<s<<"含有英文个数为:"<<yingwen<<endl;
}
}

int main()
{
char s[100];
cout<<"请输入一个字符串:";
cin.getline(s,100);
processString(s);
return 0;
}

效果如图:在字符串末尾的空格也能识别出来。



需要注意的地方:输入字符串不能使用Cin>>,因为遇到空格会自动截取。

要用cin.Getline()函数,整行读取。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: