2010-03-17 17 views
15

Domyślnie, printf() wydaje się dopasowywać łańcuchy w prawo.Łańcuchy centrujące z printf()

printf("%10s %20s %20s\n", "col1", "col2", "col3"); 
/*  col1     col2     col3 */ 

mogę także wyrównać tekst do lewej tak:

printf("%-10s %-20s %-20s", "col1", "col2", "col3"); 

Czy istnieje szybki sposób wyśrodkować tekst? Czy muszę napisać funkcję, która zmienia ciąg znaków, taki jak test w (space)(space)test(space)(space), jeśli szerokość tekstu dla tej kolumny wynosi 8?

Odpowiedz

22

printf sama w sobie nie daje rade, ale można grać z „pośredniego” szerokości, która określa szerokość przez odczytanie z argumentu. Pozwala spróbuj tego (ok, nie jest doskonały)

void f(char *s) 
{ 
     printf("---%*s%*s---\n",10+strlen(s)/2,s,10-strlen(s)/2,""); 
} 
int main(int argc, char **argv) 
{ 
     f("uno"); 
     f("quattro"); 
     return 0; 
} 
2

Brak specyfikatora formatu printf() do tekstu środkowego.

Musisz napisać własną funkcję lub zlokalizować bibliotekę, która zapewnia pożądaną funkcjonalność.

0

Tak, będziesz musiał napisać własną funkcję, która zwróci "test" itp., Np.

printf("%s %s %s", center("col1", 10), center("col2", 20), center("col3", 20)); 

Albo masz funkcję center_print, coś jak następuje:

void center_print(const char *s, int width) 
{ 
     int length = strlen(s); 
     int i; 
     for (i=0; i<=(width-length)/2; i++) { 
       fputs(" ", stdout); 
     } 
     fputs(s, stdout); 
     i += length; 
     for (; i<=width; i++) { 
       fputs(" ", stdout); 
     } 
} 
+0

Pierwsza propozycja: Jak można to impl'd bez wycieku pamięci? – kevinarpe

+0

Jeśli dokonasz wstępnej alokacji niektórych buforów na podstawie pewnych kryteriów, które nie wydają się nierozsądne (na przykład nie więcej niż 20 argumentów zostanie wyśrodkowanych dla jednego printf, a żaden z wyśrodkowanych wyników nie będzie dłuższy niż 200 bajtów), możesz pozwolić centrum funkcja tylko obróć bufory przy każdym wywołaniu. – hlovdal

0

można użyć jednej z następujących dwóch opcji:

char name[] = "Name1"; 

//Option One 
printf("%*s", 40+strlen(name)/2, name, 40-strlen(name)/2, ""); 
puts("");//skip one line 
//Option two 
printf("%*s", 40+strlen("Name2")/2, "Name2", 40-strlen("Name2")/2, ""); 

Wyjście jest:

Nazwa1 (centrum)
Nazwa2 (centrum)

0

Możesz spróbować napisać własną funkcję dla tego problemu.

/** 
* Returns a sting "str" centered in string of a length width "new_length". 
* Padding is done using the specified fill character "placeholder". 
*/ 
char * 
str_center(char str[], unsigned int new_length, char placeholder) 
{ 
    size_t str_length = strlen(str); 

    // if a new length is less or equal length of the original string, returns the original string 
    if (new_length <= str_length) 
     return str; 

    char *buffer; 
    unsigned int i, total_rest_length; 

    buffer = malloc(sizeof(char) * new_length); 

    // length of a wrapper of the original string 
    total_rest_length = new_length - str_length; 

    // write a prefix to buffer 
    i = 0; 
    while (i < (total_rest_length/2)) { 
     buffer[i] = placeholder; 
     ++i; 
    } 
    buffer[i + 1] = '\0'; 

    // write the original string 
    strcat(buffer, str); 

    // write a postfix to the buffer 
    i += str_length; 
    while (i < new_length) { 
     buffer[i] = placeholder; 
     ++i; 
    } 
    buffer[i + 1] = '\0'; 

    return buffer; 
} 

Wyniki:

puts(str_center("A", 0, '-')); // A 
puts(str_center("A", 1, '-')); // A 
puts(str_center("A", 10, '-')); // ----A----- 
puts(str_center("text", 10, '*')); // ***text*** 
puts(str_center("The C programming language", 26, '!')); // The C programming language 
puts(str_center("The C programming language", 27, '!')); // The C programming language! 
puts(str_center("The C programming language", 28, '!')); // !The C programming language! 
puts(str_center("The C programming language", 29, '!')); // !The C programming language!! 
puts(str_center("The C programming language", 30, '!')); // !!The C programming language!! 
puts(str_center("The C programming language", 31, '!')); // !!The C programming language!!!