问题描述:
输入一个字符串,以‘$’结束,分别统计各大写字母出现的次数,并按照字母出现的多少进行输出。(先输出出现次数多的,次数相同的按字母表顺序输出,不出现的字母不输出)
代码展示:
1 #include<stdio.h>
2 int main(){
3 char ch[100];
4 int num[26];
5 int len=0;
6 int i,j,max;
7
8 for(i=0;i<26;i++){
9 num[i] = 0;
10 }
11 while(1){ //读入字符
12 scanf("%c",&ch[len]);
13 len++;
14 if(ch[len-1]=='$'){
15 break;
16 }
17 }
18 for(i=0; i<len; i++){ //统计各元素的个数
19 if(ch[i]>='A' && ch[i]<='Z'){
20 num[ch[i]-'A']++;
21 }
22 }
23 for(i=0;i<26;i++){ //遍历输出
24 max = i;
25 for(j=0;j<26;j++){ //找到最大值
26 if(num[j]>num[max]){
27 max = j;
28 }
29 }
30 if(num[max] != 0){
31 printf("%c的个数为:%d\n",'A'+max, num[max]);
32 num[max]=0;
33 }
34 }
35 return 0;
36 }
运行截图: