题解 | #统计字符#

统计字符

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while(getline(cin, str))
    {
        int length = str.length();
        int iLetter=0,iSpace=0,iNum=0,iOther=0;
        for(int i=0; i<length; i++)
        {

           if(str[i]-'a'>=0 && str[i]-'z'<=0)
           {
               iLetter ++;
           }
           else if(str[i]-'0'>=0 && str[i]-'9'<=0)
           {
               iNum ++;
           }
           else if(str[i]-' '==0)
           {
               iSpace ++;
           }
           else
           {
               iOther ++;
           }
        }
        cout<<iLetter<<endl<<iSpace<<endl<<iNum<<endl<<iOther<<endl;
    }
    return 0;
}
全部评论

相关推荐

4 收藏 评论
分享
牛客网
牛客企业服务