C语言:编写一个程序,统计输入正文中每个数字字符,英文字符和其他字符出现的次数

编写一个程序,统计输入正文中每个数字字符,英文字符和其他字符出现的次数(要求用数组元素作为每个数字字符、英文字符、和其他字符出现的次数的计数器)。
输入:一段文字,以EOF结束(Crtl+Z)
输出:参照样例输出。
样例输入

"123 The day the child realizes that all adults are imperfect 456456,
he becomes an adolescent; 345 the day he forgives them, he becomes an adult; the day he forgives himself, he becomes wise." 342

样例输出

Number 0: 0
Number 1: 1
Number 2: 2
Number 3: 3
Number 4: 4
Number 5: 3
Number 6: 2
Number 7: 0
Number 8: 0
Number 9: 0
characters: 140
other: 44

//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
int main(void){
    char ch;
    int Dec[10]={0},letter=0,other=0,i=0;
    while(1){
        if((ch=getchar())>='0' && ch<='9')
            Dec[ch-'0']++;
        else if(ch>='A' && ch<='Z' || ch>='a' && ch<='z')
            letter++;
        else if(ch=='\n')
            break;
        else other++;
    }
    for(i=0;i<10;printf("Number%d: %d\n",i,Dec[i++]));
    printf("Characters: %d\n",letter);
    printf("Other: %d\n",other+1);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网