您的位置:首页 > 其它

一些练习——统计字符串中各种字符的个数

2014-12-19 22:55 232 查看
代码

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    char ch[100];
    gets(ch);
    int a=0,b=0,c=0,d=0,i=0;
    while(ch[i]!='\0')
    {
        if((ch[i]>='A'&&ch[i]<='Z')||(ch[i]>='a'&&ch[i]<='z'))
            a++;
        else if(ch[i]>='0'&&ch[i]<='9')
            b++;
        else if(ch[i]==' ')
            c++;
        else
            d++;
        i++;
    }
    cout<<"字母的个数:"<<a<<endl;
    cout<<"数字的个数:"<<b<<endl;
    cout<<"空格的个数:"<<c<<endl;
    cout<<"其他字符的个数:"<<d;
    return 0;
}


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