统计字母个数

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
",A);
    printf("b %d
",B);
    printf("c %d
",C);
    printf("d %d
",D);
    printf("e %d
",E);
    printf("f %d
",F);
    printf("g %d
",G);
    printf("h %d
",H);
    printf("i %d
",I);
    printf("j %d
",J);
    printf("k %d
",K);
    printf("l %d
",L);
    printf("m %d
",M);
    printf("n %d
",N);
    printf("o %d
",O);
    printf("p %d
",P);
    printf("q %d
",Q);
    printf("r %d
",R);
    printf("s %d
",S);
    printf("t %d
",T);
    printf("u %d
",U);
    printf("v %d
",V);
    printf("w %d
",W);
    printf("x %d
",X);
    printf("y %d
",Y);
    printf("z %d
",Z);
    return 0;
}
原文地址:https://www.cnblogs.com/tianmin123/p/4859253.html