【问题标题】:Print a array of strings nicely in C在 C 中很好地打印字符串数组
【发布时间】:2020-12-21 17:19:09
【问题描述】:

我正在使用 C 打印如此庞大的字符串数组 例如:

static const char * const fruits[] =
{   
"apple",
"banana",
"pine apple",
"two apple",
"two banana",
"two  pine apple",
"three apple",
"three banana",
"three pine apple"
};

我正在尝试迭代并连续打印数组 3 的值

void printNames(){
    int i;
    for(i = 0; i < 10 ; i++){
            if(i%3== 0){
                printf("\n");
            }
            printf("%s: %d | ",fruits[i],i);
        }
    }
    printf("\n");
}

这给了我输出:

apple:0 | banana :1 | pine apple:2 |
two apple:3 | two banana :4 | two  pine apple:5 |
three apple:6 |three banana :7 | three pine apple:8 |

我正在尝试实现这样的目标:

apple:0       | banana :1       | pine apple:2       |
two apple:3   | two banana :4   | two  pine apple:5  |
three apple:6 | three banana :7 | three pine apple:8 |

感谢任何建议和帮助 谢谢。

【问题讨论】:

  • 首先循环遍历数组以计算每列所需的宽度。然后再次循环,这次为每个输出添加所需的填充,以使每列中的所有条目具有相同的宽度。
  • 我们如何知道数组中每个元素需要多少填充?
  • 你为什么在"pine apple""two apple",之间缺少,以及另一个地方,这会导致输出错误
  • 嗨@IrAM 对不起,这是我实际问题的一个例子。编辑它。感谢您指出这一点。
  • @user3121023 该解决方案有效,有没有一种有效的方法来获得该值 20?例如,如果有一个值大于 25 个字符。它将是 20-25,结果为负数。有没有什么我们可以用来限制它只打印 20 个字符,即使它是 25 个字符并且在这种情况下不使用任何填充?

标签: c formatting printf posix


【解决方案1】:

这将遍历fruits 并根据strlen 和位数分配overall 宽度和column 宽度。还有一个字符串长度为 20 的 MAX

#include <stdio.h>
#include <string.h>

#define COLS 3
#define MAX 20

static const char * const separator = " | ";
static const char * const fruits[] = {
    "apple",
    "banana",
    "pine apple",
    "two apple",
    "two banana",
    "two  pine apple",
    "three apple",
    "three banana",
    "three pine apple",
    "four abcdefghijklmnopqrstuvwxyz",
    "four 0123456789",
    "four pine apple",
    NULL
};

int main ( void) {
    int i;
    int overall = 0;
    int column[COLS] = { 0};

    for ( i = 0; fruits[i]; i++){//fruits[i] is not NULL
        int digits = 0;
        int temp = i;
        while ( temp) {
            ++digits;
            temp /= 10;
        }

        temp = strlen ( fruits[i]);
        if ( temp > MAX) {
            temp = MAX;
        }
        temp += digits;
        if ( overall < temp) {
            overall = temp;
        }
        if ( column[i % COLS] < temp) {
            column[i % COLS] = temp;
        }
    }
    //use overall width
    for ( i = 0; fruits[i]; i++){
        if ( i % COLS == 0){
            printf ( "\n");
        }
        else {
            printf ( "%s", separator);
        }
        int len = strlen ( fruits[i]);
        if ( len >= MAX) {
            len = MAX;
        }
        int width = overall - len;
        printf ( "%.*s: %-*d", MAX, fruits[i], width, i);
    }
    printf ( "\n");
    //use column width
    for ( i = 0; fruits[i]; i++){
        if ( i % COLS == 0){
            printf ( "\n");
        }
        else {
            printf ( "%s", separator);
        }
        int len = strlen ( fruits[i]);
        if ( len >= MAX) {
            len = MAX;
        }
        int width = column[i % COLS] - len;
        printf ( "%.*s: %-*d", MAX, fruits[i], width, i);
    }
    printf ( "\n");
}

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2018-03-04
    • 1970-01-01
    • 2020-04-08
    • 2017-11-25
    • 2017-06-26
    相关资源
    最近更新 更多