Connecting Tech Pros Worldwide Forums | Help | Site Map

printf: Centre justify data

vashwath@rediffmail.com
Guest
 
Posts: n/a
#1: Dec 2 '05
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
Guest
 
Posts: n/a
#2: Dec 2 '05

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
slebetman@yahoo.com
Guest
 
Posts: n/a
#3: Dec 2 '05

re: printf: Centre justify data


vashwath@rediffmail.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.

Closed Thread