【2049】字符个数

Time Limit: 1 second
Memory Limit: 2 MB

问题描述

输入一串字符串,分别统计数字、大小写字母和其他字符的个数,最后以“?”结束。(其他字符不包含"?")
    


  

 

Input

一行,一个字符串,以“?”结束。

Output

三行,分别显示三种字符的个数

Sample Input

2s0d3s<5?

Sample Output

letter:3
digit:4
other:1

【题解】

用一个while 一个 变量i 就可以了。一直扫描到问号,之前的字符和数字用if判断一下就可以了。a[1..3]数组 用来存取各个不同的类型的数据的数量。

【代码】

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

string s1;
int a[4]; //a[1..3]分别表示字母,数字 ,其他

void input_data()
{
    char a[300];
    cin.getline(a,300); //怕有空格直接整行读取
    s1 = string(a);
    for (int i = 1;i <= 3;i++)
        a[i] = 0;
}

void get_ans()
{
    int i = 0;
    while (s1[i] != '?')
        {
            if (s1[i] >= '0' && s1[i] <= '9') //这里是判断数据的类型
                a[2]++;
                    else
                        if ((s1[i] >= 'A' && s1[i] <='Z' )|| (s1[i] >= 'a' && s1[i] <= 'z'))
                            a[1]++;
                                else
                                    a[3]++;
            i++;
        }
}

void output_ans()
{
    printf("letter:%d\n",a[1]);
    printf("digit:%d\n",a[2]);
    printf("other:%d",a[3]);
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    input_data();
    get_ans();
    output_ans();
    return 0;
}
posted @ 2017-10-06 19:23  AWCXV  阅读(278)  评论(0编辑  收藏  举报