1-13 打印输入单词长度的直方图

本文介绍了一种使用C语言编程技术来实现文本长度可视化的解决方案,通过水平和垂直两个方向展示单词长度,帮助读者理解文本结构。文章提供了详细的代码示例,包括如何读取输入、计算单词长度以及打印可视化结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水平方向

#include <stdio.h>
#include <stdbool.h>

#define MAXWORDCOUNT 10

int main(int argc, char **argv) {
    int arr[MAXWORDCOUNT];
    char c;
    for (int i = 0; i< MAXWORDCOUNT; ++i) {
        int count = 0;
        bool start = false;
        while ((c = getchar()) != EOF) {
            if ( c != ' ' && c != '\n' && c != '\t' && start == false) {
                ++count;
                start = true;
            } else {
                if (c ==' ' || c == '\n' || c == '\t') {
                    arr[i] = count;
                    break;
                } else {
                        ++count;
                }
            }
        }
    }
    
    for (int i = 0; i < MAXWORDCOUNT; ++i) {
        printf("%d line: ", i);
        for (int j = 0; j < arr[i]; ++j) {
            printf("*");
        }
        printf("\n");
    }
}
helicopter
watermallon
apple
close
book
egg
eat
water
flower
computer
0 line: **********
1 line: ***********
2 line: *****
3 line: *****
4 line: ****
5 line: ***
6 line: ***
7 line: *****
8 line: ******
9 line: ********

垂直方向

#include <stdio.h>
#include <stdbool.h>

#define MAXWORDLEN 10
#define MAXWORDCOUNT 10

char record[MAXWORDLEN][MAXWORDCOUNT];
int wordLenArr[MAXWORDCOUNT];

int main(int argc, char **argv) {
    for(int i = 0; i < MAXWORDCOUNT; ++i) {
        char c;
        while ((c = getchar()) != ' ' && c != '\t' && c != '\n' && c != EOF) {
            ++wordLenArr[i];
        }
    }

    for (int i = 0; i < MAXWORDLEN; ++i) {
        for (int j = 0; j < MAXWORDCOUNT; ++j) {
            if (wordLenArr[i] >= 0) {
                record[MAXWORDLEN - j][i] = '*';
                --wordLenArr[i];
            } else {
                record[MAXWORDLEN - j][i] = ' ';
            }
        }
    }

    for (int i = 0; i < MAXWORDLEN; ++i) {
        for (int j = 0; j < MAXWORDCOUNT; ++j) {
            printf("%c\t", record[i][j]);
        }
        printf("\n");
    }

    for (int i = 0; i <= MAXWORDCOUNT; ++i) {
        printf("line%d  ", i);
    }
    printf("\n");

    return 0;
}

apple
book
first
second
third
room
water
floor
pen
mirror

                                                                         
                                                                         
                                                                         
                        *                                               *
*               *       *       *               *       *               *
*       *       *       *       *       *       *       *               *
*       *       *       *       *       *       *       *       *       *
*       *       *       *       *       *       *       *       *       *
*       *       *       *       *       *       *       *       *       *
line0  line1  line2  line3  line4  line5  line6  line7  line8  line9  line10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值