Aligning strings the same

I am trying to align these lines with their respective indexes. The lines look like this:

char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};

and my conclusion is as follows:

Fred 0
Augustine 1
Bob 2
Lenny 3
Ricardo 4

But I'm for something like this:

Fred       0
Augustine  1
Bob        2
Lenny      3
Ricardo    4

As a result, the indices are aligned the same way. I'm just trying to make it more readable. This my code looks like this:

#include <stdio.h>
#include <stdlib.h>

int
main(void) {
    int i;
    char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};

    for (i = 0; i < 5; i++) {
        printf("%s %d\n", mystrings[i], i);
    }

    return 0;
}

Is there a way I can do this? How to compare indices with the longest string and then create spaces for such integers?

+4
source share
3 answers

Extra loop to calculate the maximum width, then use the *printf flag to take the width from the argument (which is multiplied by -1 to cause left alignment)

char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};
int width = 0;
for (int i = 0; i < sizeof(mystrings)/sizeof(char*); i++) {
    width = strlen(mystrings[i]) > width? strlen(mystrings[i]) : width;
}

for (int i = 0; i < sizeof(mystrings)/sizeof(char*); i++) {
    printf("%*s %d\n", -1 * width, mystrings[i], i);
}
+6
source

.
-, , , .

printf("%-10s %d\n", mystrings[i], i);

10 mystrings[i] .

, . , , .

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

#define MAXSIZE 5

int main(void) {
   int i;
   char *mystrings[MAXSIZE] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};

   int maxLen = 0;
   for (i = 0; i < MAXSIZE; i++) {
      int len = strlen(mystrings[i]);
      maxLen = (len > maxLen) ? len : maxLen;
   }

   char formatString[50];
   sprintf(formatString, "%%-%ds %%d\n", maxLen + 1);

   for (i = 0; i < MAXSIZE; i++) {
      printf(formatString, mystrings[i], i);
   }

   return 0;
}

Fred       0
Augustine  1
Bob        2
Lenny      3
Ricardo    4
+2

, . , , , ( ) . printf , .

:

Fred      0
Augustine 1
Bob       2
Lenny     3
Ricardo   4

:

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

static size_t max_len(const char * const data[], size_t size) {
    size_t i = 0;
    size_t max = strlen(data[0]);

    for(i = 1; i < size; i++) {
        size_t len = strlen(data[i]);

        if(len > max) {
            max = len;
        }
    }
    return max;
}

int main(void) {
    const char *const mystrings[] = {
        "Fred", "Augustine", "Bob", "Lenny", "Ricardo"
    };

    size_t m = max_len(mystrings, 5);
    size_t i;

    for(i = 0; i < 5; i++) {
        const char *item = mystrings[i];
        size_t len = strlen(item);
        size_t padding = 0;
        /* +1 for padding byte */
        if(len + 1 < m) {
           padding = m - len;
        }

        printf("%s ", item);

        size_t ii;

        for(ii = 0; ii < padding; ii++) {
            fputc(' ', stdout);
        }

        printf("%zu\n", i);
    }

    return 0;
}
+2

Source: https://habr.com/ru/post/1654762/


All Articles