2010-03-17 180 views
15

默認情況下,printf()似乎將字符串對齊到右側。使用printf()對齊字符串

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

我也可以對齊文本向左這樣的:

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

是否有一個快速的方法來居中文本?或者,如果該列的文本寬度是8,那麼我是否必須編寫一個函數將test這樣的字符串轉換爲(space)(space)test(space)(space)

回答

22

的printf本身不能做的伎倆,但你可以用「間接」的寬度,它指定播放從參數中讀取寬度。讓我們試試這個(ok了,不完美)

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

中間文本沒有printf()格式說明符。

您需要編寫自己的函數或找到提供所需功能的庫。

0

是的,你將不得不編寫你自己的函數返回「測試」等,例如,

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

或者你有一個center_print功能,類似如下:

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

第一個建議:這怎麼能不泄露內存中impl'd? – kevinarpe 2017-03-31 13:57:05

+0

如果根據一些看起來不合理的標準預先分配一些緩衝區(例如,對於一個printf不會有超過20個參數將居中,並且沒有中心結果會超過200個字節),那麼可以讓中心函數只是在每次調用時旋轉緩衝區。 – hlovdal 2017-04-02 15:18:45

0

您可以使用以下兩種選項之一:

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, ""); 

輸出是:

名1(中心)
名稱2(中)

0

您可以嘗試爲此問題編寫自己的功能。

/** 
* 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; 
} 

結果:

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!!!