printf: Centre justify data

V

vashwath

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.
 
G

Gordon Burditt

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.

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
 
S

slebetman

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.
Good.

But I need to center other data types too. At least give me some
hint to do this if not the exact code.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top