Results 1 to 2 of 2

Thread: printf() and Centered Output?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    printf() and Centered Output?

    Is there any way to center align the output of printf()?

    Code:
    printf("%20s\n", "Monkey Poo");
    This will right align the output. I think + will left align it. Is there any way to center it?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: printf() and Centered Output?

    PHP Code:
    /* align.c: Align a string within a background */

    #include <string.h>

    #define LEFT (-1)
    #define CENTER 0
    #define RIGHT 1

    #define min(x,y) ((x) <= (y) ? (x) : (y))

    char *align(char *buf,int width,char fill,int justify,char *data)
    {
       
    char *p;
       
       
    /* Truncate, if necessary */
       
    int dlen min(width,strlen(data));
       
       
    /* Populate with fill character */
       
    memset(buf,fill,width);
       
    buf[width] = '\0';
       
       
    /* Calculate starting point */
       
    if (justify == LEFT)
          
    buf;
       else if (
    justify == CENTER)
          
    buf + (width-dlen)/2;
       else
          
    buf width-dlen;
       
       
    /* Insert the data there */
       
    memcpy(p,data,dlen);
       return 
    buf;


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width