C ++は、大文字、小文字、スペース、数字、その他の文字の数を調べます

C ++は、大文字、小文字、スペース、数字、その他の文字の数を調べます

タスクの説明

テキスト行を入力して、大文字、小文字、スペース、数字、およびその他の文字(ポインターまたは参照メソッドが必要)の数を調べます。

テスト入力:

abcJK 116_

予想される出力:

upper case:2
lower case:3
space:1
digit:3
other:1

テスト入力:

QWERzxcv@#$7 8*(zz

予想される出力:

upper case:4
lower case:6
space:1
digit:2
other:5

ソースコード:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;


int main()
{
    string str;
    int lowersum=0,uppersum=0,space=0,digitsum=0,othersum=0;
    int i;
    getline(cin,str);
    for(i=0;i<str.length();i++){
        if(str[i]>='a'&&str[i]<='z')lowersum++;
        else if(str[i]>='A'&&str[i]<='Z')uppersum++;
        else if(str[i]==' ')space++;
        else if(str[i]>='0'&&str[i]<='9')digitsum++;
        else othersum++;
    }
    cout<<"upper case:"<<uppersum<<endl;
    cout<<"lower case:"<<lowersum<<endl;
    cout<<"space:"<<space<<endl;
    cout<<"digit:"<<digitsum<<endl;
    cout<<"other:"<<othersum<<endl;
    return 0;
}

おすすめ

転載: www.cnblogs.com/lightice/p/12691758.html