Instead of adding spaces, you could use the field width specifier.
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *text[] =
{
"a string",
"another string",
"the longest string",
"a shorter string",
};
size_t i, length [ sizeof(text)/sizeof(*text) ] = {0}, longest = 0;
/* find the longest string */
for(i = 0; i < sizeof(text)/sizeof(*text); ++i)
{
length[i] = strlen(text[i]);
if(length[i] > longest)
{
longest = length[i];
}
}
/* specify the field width as the length of the longest string */
printf("%-*s %s\n", (int)longest, "text", "length");
for(i = 0; i < sizeof(text)/sizeof(*text); ++i)
{
printf("%-*s %lu\n", (int)longest, text[i], (unsigned long)length[i]);
}
return 0;
}
/* my output
text length
a string 8
another string 14
the longest string 18
a shorter string 16
*/