编写一个程序,打印输入中单词长度的直方图。

这道题是C程序语言设计中的练习1-13.

只是基本实现,若有错误或更优秀的方法,欢迎指正,谢谢。

习题1-14类似,依次小改即可。

#include <stdio.h>

main()
{
    int i;
    int c;
    int num = 0;
    int a = 0;//字母个数
    int b[10];//数字个数
    int d = 0;//空格个数
    int e = 0;//其他字符个数
    
    //对数组进行赋值
    for(i = 0; i < 10; ++i){
        b[i] = 0;
    }
    
    //开始进行字母类型判断
    while ((c = getchar()) != EOF){
        
        //当输入为英文字母时
        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
            ++a;
        //当输入为数字时
        else if (c >= '0' && c <= '9')
            ++b[c-'0'];
        //当输入为空格、换行符、制表符时
        else if (c == ' ' || c == '\n' || c == '\t')
            ++d;
        //其他输入
        else
            ++e;
            
            }
    //计算输入数字总个数
    for(i = 0; i < 10; ++i)
        num += b[i];
    //打印字母个数
    printf("\nEN_words:  ");
    for(i=0;i<a;i++)
        printf("*");
    printf(" %d", a);
    //打印数字个数
    printf("\nnumbers:   ");
    for (i=0; i<num;i++) {
        printf("*");
    }
    printf(" %d", num);
    //打印空格及换行的个数
    printf("\nempty:     ");
    for (i=0; i<d; i++) {
        printf("*");
    }
    printf(" %d", d);
    //打印其他字符出现个数
    printf("\nothers:    ");
    for (i=0; i<e; i++) {
        printf("*");
    }
    printf(" %d", e);
    
    printf("\n");
    
    return 0;
}

最后是实例图:


猜你喜欢

转载自blog.csdn.net/secret_lee/article/details/80023003
今日推荐