printf: Centre justify data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vashwath@rediffmail.com

    #1

    printf: Centre justify data

    Hi all,
    Is there any way to print the data in the center of the field width? I
    have searched in internet and I got code for center justifying string
    data. But I need to center other data types too. At least give me some
    hint to do this if not the exact code.

  • Gordon Burditt

    #2
    Re: printf: Centre justify data

    >Is there any way to print the data in the center of the field width? I[color=blue]
    >have searched in internet and I got code for center justifying string
    >data. But I need to center other data types too. At least give me some
    >hint to do this if not the exact code.[/color]

    You have a field width W. You have a string of width S. You need
    W-S padding spaces (if it's negative, you're in trouble!). Put
    L=(W-S)/2 padding spaces on the left, and R=(W-S)-L padding spaces
    on the right.

    If you need to center other data types, well, sprintf() returns
    the length of the string it's going to generate. If you have
    snprintf() available (C99, not C89) you might consider using it.

    Gordon L. Burditt

    Comment

    • slebetman@yahoo.com

      #3
      Re: printf: Centre justify data

      vashwath@rediff mail.com wrote:[color=blue]
      > Hi all,
      > Is there any way to print the data in the center of the field width? I
      > have searched in internet and I got code for center justifying string
      > data.[/color]

      Good.
      [color=blue]
      > But I need to center other data types too. At least give me some
      > hint to do this if not the exact code.[/color]

      All other data types can be converted to string data using sprintf().
      Once this is done use the code you got for center justifying strings.

      Comment

      Working...