zoukankan      html  css  js  c++  java
  • 单词长度统计,字符数量统计直方图

    1. 打印输入中单词长度

    2. 打印输入中各个字符出现频度的直方图

    #include <stdio.h>
    #include <Conio.h>
    #include <stdlib.h>
    #define IN 1
    #define OUT 0
    main()
    {
        int nc, nw, count, state, input;
        int nclist[26];
        
        for (int i = 0; i< 26; ++i){
            nclist[i] = 0;
        }
        
        nc = nw = count = 0;
        int* nwlist = (int*)calloc(nw,sizeof(int));
        state = OUT;
        while((input=getchar())!=EOF){
            ++nc;
            ++nclist[input-'a'];
            if (state == IN){
                nwlist [nw-1] = ++count;
            }
            if(input == ' ' || input == '	'|| input == '
    '){
                state = OUT;
                count = 0;
            }else if(state == OUT){
                state = IN;
                ++nw;
                nwlist = (int *)realloc(nwlist, 1*sizeof(int));
            }
        }
        printf("************below is word length details***********
    ");
        for(int i = 0; i < nw; ++i){
            printf("%d
    ",nwlist[i]);
        }
        printf("************below is character details************
    ");
        for(int i = 0; i < 26; ++i){
            printf("%c: ", ('a'+ i));
            for(int j = 0; j< nclist[i]; ++j){
                printf("*");
            }
            printf("
    ");
        }
        getch();     
    }

    结果:

    tips:

    C语言中数组动态分配int* a=(int*)calloc(n,sizeof(int));//n是数组的大小。

    需要导入<stdlib.h>包。本例在不考虑效率的情况下也可以申请一个足够大的数组。

  • 相关阅读:
    学习Python第三天
    学习Python第二天
    学习Python第一天
    centos7 系统优化
    crond计划任务
    day2
    day1
    A.浏览器访问 kube-apiserver 安全端口
    12.清理集群
    11.部署 harbor 私有仓库
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4497593.html
Copyright © 2011-2022 走看看