Center Justification for C - printf

I was working on a project usig an avr controller and glcd for showning the user options menu. For the menu entries we were using sprintf function to create a string and then passing it to the glcd library functions. Inorder to align the printed text, I searched for the justification options availale with printf/sprintf functions.
After a lot of searching, I found that only left justification and right justification supported, so we have to use some tecniques or custom functions to align the text. Finally from a forum, I got a smart piece of code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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; 
}

No comments :

Post a Comment