题解 | #统计字符#

统计字符

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

#include <iostream>
#include <string>

using namespace std;

/*
思路:根据英文字符、数字、空格、其他进行分别统计
*/
void accountCharacters(string strIn)
{
    int englishCharaNums = 0, digitNums = 0, spaceNums = 0, otherNums = 0;
    
    for(int i = 0; i < strIn.length(); i++) {
        if(strIn[i] >= 'a' && strIn[i] <= 'z' || strIn[i] >= 'A' && strIn[i] <= 'Z') {
            englishCharaNums++;
        } else if (strIn[i] >= '0' && strIn[i] <= '9') {
            digitNums++;
        } else if(strIn[i] == ' ') {
            spaceNums++;
        } else {
            otherNums++;
        }
        
    }
    cout<<englishCharaNums<<endl;
    cout<<spaceNums<<endl;
    cout<<digitNums<<endl;
    cout<<otherNums<<endl;
    
}
int main()
{
    
    string strIn;
    while(getline(cin, strIn)) {
       accountCharacters(strIn);
    }
}
全部评论

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务