字母统计

题目描述

输入一行字符串,计算其中A-Z大写字母出现的次数

输入描述:

案例可能有多组,每个案例输入为一行字符串。

输出描述:

对每个案例按A-Z的顺序输出其中大写字母出现的次数。

分析:

对每一个出现的字符用数组count[26]的一个元素统计

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main(){
    string str;
    int count[26];
    for(int i = 0; i < 26; i++)
        count[i] = 0;
    while(cin >> str){
        for(int i = 0; i < str.size(); i++){
            if(str[i] >= 'A' && str[i] <= 'Z')
                count[str[i] - 'A']++;
        }
        for(int i = 0; i < 26; i++){
           printf("%c:%d
", 'A' + i, count[i]);
            //cout << 'A' + i << ":" << count[i] << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhuobo/p/10201750.html