题解 | #统计字符#15行代码

统计字符

http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
//数据范围:输入的字符串长度满足 1 \le n \le 1000 \1≤n≤1000 
// 输入描述:
// 输入一行字符串,可以有空格
// 输出描述:
// 统计其中英文字符,空格字符,数字字符,其他字符的个数
using namespace std;

int main()
{
    string str;// 输入一行字符串,可以有空格
    while(getline(cin,str))//此处注意不能用cin>>str替代
    {
        int  iCount1=0,iCount2=0,iCount3=0,iCount4=0;//英文字符,空格字符,数字字符,其他字符的个数
        int len = str.length();
        for(int i=0;i<len;i++)
        {
            if(isalpha(str[i]))
                iCount1++;
            else if(str[i] >= '0' && str[i] <= '9')
                iCount3++;
            else if(str[i] == ' ')
                iCount2++;
            else
                iCount4++;//其他字符个数
        }
        cout<<iCount1<<"\n"<<iCount2<<"\n"<<iCount3<<"\n"<<iCount4<<endl;
//         cout<<iCount1<<endl;
//         cout<<iCount2<<endl;
//         cout<<iCount3<<endl;
//         cout<<iCount4<<endl;
        
    }
    //cout<<endl;
    return 0;
}
全部评论

相关推荐

昨天 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务