简体   繁体   English

如何连接字符串以生成固定宽度的输出

[英]How do I concatenate strings to generate fixed width output

I have an array of structures, where each structure contains a first name and a last name, along with other information. 我有一个结构数组,其中每个结构都包含一个名字和一个姓氏以及其他信息。 I'm trying to print the array in tabular form, something like this 我正在尝试以表格形式打印数组,像这样

+---------------------------+--------+--------+
| Student Name              | Test 1 | Test 2 |
+---------------------------+--------+--------+
| Pousseur, Henri           |   95   |   92   |
| Boyer, Charles            |   90   |   97   |
+---------------------------+--------+--------+

However, my output looks like this 但是,我的输出看起来像这样

+---------------------------+--------+--------+
| Student Name              | Test 1 | Test 2 |
+---------------------------+--------+--------+
| Pousseur, Henri                  |   95   |   92   |
| Boyer, Charles                     |   90   |   97   |
+---------------------------+--------+--------+

My question is, how do I concatenate the last name to the first name (with a comma in between), and then print the whole thing as a fixed width string? 我的问题是,如何将姓氏与名字(用逗号隔开)连接起来,然后将整个内容打印为固定宽度的字符串? So that my table lines up correctly. 这样我的桌子就可以正确排列了。

Here's my code: 这是我的代码:

#include <stdio.h>
#define NAME_LEN 25

typedef struct
{
    char fname[NAME_LEN+1];
    char lname[NAME_LEN+1];
    int score1;
    int score2;
} STUREC;

void printRecords(STUREC records[], int count)
{
    printf("+---------------------------+--------+--------+\n");
    printf("| Student Name              | Test 1 | Test 2 |\n");
    printf("+---------------------------+--------+--------+\n");

    for (int i = 0; i < count; i++)
        printf ("| %-s, %-26s| %4d   | %4d   |\n", records[i].lname, records[i].fname, records[i].score1, records[i].score2 );

    printf("+---------------------------+--------+--------+\n");
}

int main(void)
{
    STUREC records[] = {
        { "Henri"  , "Pousseur", 95, 92 },
        { "Charles", "Boyer"   , 90, 97 }
    };

    printRecords(records, 2);
}

Note that printf returns the number of characters printed. 请注意, printf返回打印的字符数。 So if you want to print the name within a fixed width, you can print the name, and then output spaces as needed to fill the width. 因此,如果要在固定宽度内打印名称,可以打印名称,然后根据需要输出空格以填充宽度。

#define WIDTH 26

int count = printf( "%s, %s", records[i].lname, records[i].fname );
if ( count < WIDTH )
    printf( "%*s", WIDTH-count, "" );

In the format string "%*s" , the * tells printf that the width of the string will be passed as an argument. 在格式字符串"%*s"*告诉printf该字符串的宽度将作为参数传递。 The WIDTH-count argument is the number of spaces that need to be printed, and "" is just an empty string. WIDTH-count参数是需要打印的空格数, ""只是一个空字符串。 So for example, if count is 16, then WIDTH-count is 10, so the printf is equivalent to 因此,例如,如果count为16,则WIDTH-count为10,因此printf等于

printf( "%10s", "" ); 

which will print 10 spaces. 将打印10个空格。

This is just an adpatation of @user3386109 answer. 这只是@ user3386109答案的补充。

Add

#define WIDTH 27 // You get a max width of 26

somewhere in the beginning of your program and a string 在程序开头的某个地方和一个字符串

char fullname[WIDTH];

somewhere in the beginning of the main. 在主要开始的某个地方。

Then make changes like below : 然后进行如下更改:

printf("\n+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");
    printf("| Student Name              |   ID   | Test 1 | Test 2 | Proj 1 | Proj 2 | Proj 3 | Average | Grade |\n");
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    for (i = 0; i < count; i++)
    {
       int lname_len=strlen(records[i].lname);
       int fname_len=strlen(records[i].fname);
        snprintf(fullname,WIDTH,"%s,%s%*s", records[i].lname,records[i].fname,(WIDTH>(lname_len+fname_len)?(WIDTH-(lname_len+fname_len)):0),"");

        /* The above step concatenates - since you're very interested in it -
         * the sirname and the first name into a string fullname making sure
         * that the string is padded to 26 characters utmost.
         */

        printf ("| %s | %-7.6d| %4d   | %4d   | %4d   | %4d   | %4d   | %6.2f  |  %-2s   |\n", fullname, records[i].id, records[i].score1,
                 records[i].score2, records[i].score3, records[i].score4, records[i].score5,
                  records[i].ave, records[i].grade);
    }
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    printf("\nHave A Terrible Day!\n\n");

Note the ternary expression : 注意三元表达式:

(WIDTH>(lname_len+fname_len)?(WIDTH-(lname_len+fname_len)):0)

in snprintf . snprintf This will be evaluated to unused width, if any which we pad with emptry strings "" or null characters. 如果我们使用空字符串""或空字符填充了该宽度,它将被评估为未使用的宽度。

If you didn't understand it please have a look here 如果您不明白,请在这里看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM