Center printf

2

Personal how do I centralize messages, simple texts?

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

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

    
asked by anonymous 06.06.2017 / 04:47

2 answers

0

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

Perform the procedure in printf:

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

The function to centralize follows:

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 questioner, the form that best suits you 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: link

    
06.06.2017 / 13:36
-1

Try to use a specific number of tabs \t , usually organize the result of the code a little on the console, it's not a matter of centering, but it shows similar results

printf("\t\tCLÍNICA DE ANÁLISES LABORATORIAIS\n\n");
printf("\t\tTRIAGEM ADMINISTRATIVA\n\n");
    
07.06.2017 / 17:27