简体   繁体   中英

C text alignment with padding

I have a simple function to print text.

while (recvbuf[i] != '\0') {
    if (i == 0) {
        printf("%+40c", recvbuf[i]);
    }
    else {
        printf("%c", recvbuf[i]);
    }
    i++;
};

It prints text and first word(line) starts with padding 40. But what I want to do is to start every line of this with padding 40 not only first. Don't know how to do it.

Thanks for any help.

What i need as output in CONSOLE.

             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
             fksdpo gfdg fd gdf gdf gdf gfd gdf gd

What i have now.

             fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd
fksdpo gfdg fd gdf gdf gdf gfd gdf gd

I'm not sure that it's padding you want (for me, padding comes after, so I won't use this terme). You want to add some space formatting at starting of console line.

So you must define: the console width, the starting spaces you want.

Then, for each word to print, you must calculate if the console width well be reach. If so, you must start a new line.

If a new line is needed, you must start it with spaces.

#include <ctype.h>
#include <stdio.h>

int word_length(const char *word)
{
    int size = 0;
    for (; *word && !isspace(*word); ++word)   
{     
        ++size;
    }
    return size;
}

void print_word(const char *word, int size)
{
    while(size--)
        putchar(*word++);

    putchar(' ');

}

void format(int width, int spaces, const char *text)
{
    int sol = spaces;
    int current_pos = 0;

    while (*text)
    {   
        /* size of the current word */
        int word_len;

    /* if needed, print spaces */
        for (; sol != 0; --sol)
        {
            putchar(' ');
            ++current_pos;
        }

        /* get size of next word */
        word_len = word_length(text);

        if (word_len > width)
        {
            printf("error, word too long...\n");
            return;
        }


    /* see if writting current word will go over width*/
        if (current_pos + word_len > width)
        {
            sol = spaces;
            current_pos = 0;
            putchar('\n');
            continue;
        }

        /* print the current word */
        print_word(text, word_len);

        /* update the cursor position */
        current_pos += word_len + 1;

        /* position text pointer position on next word */
        text += word_len;            
        while(*text && isspace(*text))
        {
            ++text;
        }                        
    }
}

int main(void)
{
    char text[] = "fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd fksdpo gfdg fd gdf gdf gdf gfd gdf gd";
    format(45, 5, text);
    return 0;
}

Will give:

     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 
     fksdpo gfdg fd gdf gdf gdf gfd gdf gd 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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