统计字母个数

1164: 统计字母个数

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 890  Solved: 437
[Submit][Status][BBS]

Description

给定一段文章,请输出每个字母出现的次数

Input

只有一组输入数据,该数据大小<10KB。在文章中除最后一个字符外,只有小写字母、空格和换行符,没有另外的标点、数字和大写字母等。该文章以’#’结尾。

Output

输出格式为“C A”,C为’a’..’z’中的字母,A为出现次数,C和A之间空一格

Sample Input

here is the input
this is the article#

Sample Output

a 1
b 0
c 1
d 0
e 5
f 0
g 0
h 4
i 5
j 0
k 0
l 1
m 0
n 1
o 0
p 1
q 0
r 2
s 3
t 5
u 1
v 0
w 0
x 0
y 0
z 0

HINT

Source

吉首大学软件学院

#include <stdio.h>
#include <string.h>
char a[13000];
int  main()
{
    int A=0,B=0,C=0,D=0,E=0,F=0,G=0,H=0,I=0,J=0,K=0,L=0,M=0,
        N=0,O=0,P=0,Q=0,R=0,S=0,T=0,U=0,V=0,W=0,X=0,Y=0,Z=0;
    while(1)
    {
        gets(a);//遇到回车进行一次计数,但还要进入循环再次输入
        int flag = 0;
        int len=strlen(a);
        for(int i=0; i<len; i++)
        {
            if(a[i]=='#')
            {
                flag=1;
                break;
            }
            if(a[i]=='a') A++;
            if(a[i]=='b') B++;
            if(a[i]=='c') C++;
            if(a[i]=='d') D++;
            if(a[i]=='e') E++;
            if(a[i]=='f') F++;
            if(a[i]=='g') G++;
            if(a[i]=='h') H++;
            if(a[i]=='i') I++;
            if(a[i]=='j') J++;
            if(a[i]=='k') K++;
            if(a[i]=='l') L++;
            if(a[i]=='m') M++;
            if(a[i]=='n') N++;
            if(a[i]=='o') O++;
            if(a[i]=='p') P++;
            if(a[i]=='q') Q++;
            if(a[i]=='r') R++;
            if(a[i]=='s') S++;
            if(a[i]=='t') T++;
            if(a[i]=='u') U++;
            if(a[i]=='v') V++;
            if(a[i]=='w') W++;
            if(a[i]=='x') X++;
            if(a[i]=='y') Y++;
            if(a[i]=='z') Z++;
        }
        if(flag==1)//连续跳出两次才跳得出
            break;
    }
    printf("a %d\n",A);
    printf("b %d\n",B);
    printf("c %d\n",C);
    printf("d %d\n",D);
    printf("e %d\n",E);
    printf("f %d\n",F);
    printf("g %d\n",G);
    printf("h %d\n",H);
    printf("i %d\n",I);
    printf("j %d\n",J);
    printf("k %d\n",K);
    printf("l %d\n",L);
    printf("m %d\n",M);
    printf("n %d\n",N);
    printf("o %d\n",O);
    printf("p %d\n",P);
    printf("q %d\n",Q);
    printf("r %d\n",R);
    printf("s %d\n",S);
    printf("t %d\n",T);
    printf("u %d\n",U);
    printf("v %d\n",V);
    printf("w %d\n",W);
    printf("x %d\n",X);
    printf("y %d\n",Y);
    printf("z %d\n",Z);
    return 0;
}

 

posted on 2015-10-07 19:55  甜蜜蜜吖甜蜜蜜  阅读(1035)  评论(0编辑  收藏  举报

导航