Search code examples
cformattingprintfposix

Print a array of strings nicely in C


I am working with C printing the array of strings which is so huge Ex:

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

I am trying to iterate and print the values of array 3 in a row

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

This gives me the output:

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 |

I am trying to achieve something like this:

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 |

Any suggestions and help is appreciated Thanks.


Solution

  • This iterates through fruits and assigns an overall width and a column width based on strlen and the number of digits. There is also a MAX of twenty for the length of a string.

    #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");
    }