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
今日推荐