Centralize printf

Asked

Viewed 5,096 times

3

Personal how do I center messages, simple texts?

printf("CLÍNICA DE ANÁLISES LABORATORIAIS\n\n");
printf("TRIAGEM ADMINISTRATIVA\n\n");

These two lines above are within the main. I’d like to center them on any console.

  • 4

    Center where? What is the line size? Can it change? It’s pure mathematics.

  • 1

    Follow this question. This might help: https://stackoverflow.com/questions/2461667/centering-strings-with-printf

3 answers

0

Unfortunately there is no feature in the printf to center the string, and the handling should be done manually

Perform the procedure on the printf:

printf("%", center_print("CLÍNICA DE ANÁLISES LABORATORIAIS\n\n",20));
printf("%", center_print("TRIAGEM ADMINISTRATIVA\n\n",20));

Follow the function to center:

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);
        }
}

As discussed with the author of the question, the form that suits him the most is the following solution:

 printf("%*s", 40+strlen("CLÍNICA DE ANÁLISES LABORATORIAIS\n\n"));
 printf("%*s", 40+strlen("TRIAGEM ADMINISTRATIVA\n\n"));

ref: https://stackoverflow.com/questions/2461667/centering-strings-with-printf

  • That one center of printfis a function?

  • I had made a mistake in assembling the answer, I edited it so you could understand. Unfortunately I have no way to test at the moment, anything just let me know.

  • So more without the function there does not work? I did not understand that your answer there.

  • I want to perform the direct procedure on printf and not create function.

  • 1

    Yes only works with the function, to perform directly on the printf only if you did something like this: printf("%*s", 40+strlen("Text"));

  • Until it worked, except that the following is according to the format of the screen, I had to maximize the screen to see the result.

  • Try lowering the 40 and see if it doesn’t get better, like I said, it’s a very manual process

  • I get it, but then what if I maximize it gets static, doesn’t change, get it? I don’t think there’s any way to do it.

  • 1

    I typed that you suggested: printf("%*s", 40+strlen("CLÍNICA DE ANÁLISES LABORATORIAIS\n\n")); and it’s gone, the phrase is gone.

Show 5 more comments

0

It would be necessary to make a function to generate the centralized string.

I did this job centerAlignText using the function sprintf to mount the string (you have some examples of how to use the main):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char *centerAlignText(char *field, unsigned int fieldWidth, const char *text){
    if (fieldWidth == 0 || field == NULL) return "";

    fieldWidth--;
    unsigned int len = strlen(text);
    unsigned int padding = fieldWidth > len ? (fieldWidth+1 - len)/2 : 0;
    sprintf(field, "%*s%.*s%*s", padding, "", fieldWidth-1, text, padding>0 && len%2!=fieldWidth%2 ? padding-1 : padding, "");

    return (const char *)field;
}

int main()
{
    char texto[] = "Ola Mundo!!";
    char campo[76];

    //Usa assim:
    centerAlignText(campo, sizeof campo, texto);
    printf("|_%s_|\n\n", campo);

    //Ou assim:
    printf("|_%s_|\n\n", centerAlignText(campo, sizeof campo, "Ola Mundo!"));

    //Com campo menor do que o texto a funcao corta o texto:
    char campo2[8];
    centerAlignText(campo2, sizeof campo2, texto);
    printf("|_%s_|\n", campo2);

    return 0;
}

Note that it is necessary to define the field where the text will be centered by creating a char vector of size+1 to use in the function.

-1

Try using a specific number of tabs \t, generally organizes a little the result of the code in the console, it is not really a matter of centralizing, but presents similar results

printf("\t\tCLÍNICA DE ANÁLISES LABORATORIAIS\n\n");
printf("\t\tTRIAGEM ADMINISTRATIVA\n\n");
  • Oxi is a response attempt...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.