题目1098:字母统计
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:3035
解决:1439
-
题目描述:
-
输入一行字符串,计算其中A-Z大写字母出现的次数
-
输入:
-
案例可能有多组,每个案例输入为一行字符串。
-
输出:
-
对每个案例按A-Z的顺序输出其中大写字母出现的次数。
-
样例输入:
-
DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK
-
样例输出:
-
A:0 B:0 C:0 D:3 E:2 F:5 G:0 H:0 I:1 J:4 K:2 L:3 M:0 N:1 O:0 P:0 Q:1 R:0 S:2 T:0 U:0 V:0 W:1 X:0 Y:0 Z:0
-
来源:
- 2009年上海交通大学计算机研究生机试真题
-
- c++代码:
-
//1098 字母统计 #include<string> #include<cstdio> #include<cstring> #include<iostream> using namespace std; const int MAX = 27; int hash[MAX]; int main(void) { string str; while(getline(cin,str)) { memset(hash,0,sizeof(hash)); string::iterator iter; for(iter=str.begin(); iter!=str.end(); ++iter) { if(*iter <= 'Z' && *iter >= 'A') { hash[*iter-'A']++; } } for(int i=0; i<26; ++i) { printf("%c:%d\n",'A'+i,hash[i]); } } return 0; }