how to print the output from right to left

Apr 12, 2010 at 9:45am
example:
1
2
3
4
printf("Result");
printf("123\n");
printf("1234\n");
printf("12345\n");


the result will be
Result
123
1234
12345


If i want to make the output to be like

 123
 1234
12345



Can anyone guide me?
Last edited on Apr 12, 2010 at 9:53am
Apr 12, 2010 at 9:53am
Yes.
printf("$20s", "a string");
            a string

It's right-aligned with spaces if it isn't 20 or more chars long. You can use a - sign for a left-align.
Last edited on Apr 12, 2010 at 9:54am
Apr 12, 2010 at 9:57am
:) okok.
Apr 12, 2010 at 10:07am
Hmm...... just input the spaces before the numbers and align them the way you want.

1
2
3
4
5
printf("Result");
printf("     123\n");
printf("   1234\n");
printf(" 12345\n");

but if you stored the numbers in a variable(for e.g result) then use:

 
printf("Result:  %5d",result);


then you.ll have all outputs frm 'result' left-aligned... try it let me knw if ya still hve problem.
Apr 12, 2010 at 10:14am
@ManuAizen,
1.
just input the spaces before the numbers and align them the way you want.

Yes, let's write specific code that can only be used in certain cases
2. I already said that. Also, you can use a temporary string or number for the variable.
3. It does not left align it. It right-aligns it. You have to put "%-5d" for a left-align.

I should have mentioned that this works for all the formats that printf() (actually, vsprintf()) accepts.
Last edited on Apr 12, 2010 at 10:15am
Apr 14, 2010 at 9:07am
@ chrisname,

hmm, yes indeed, sorry i forgot but the ' - '. my mistake.
Topic archived. No new replies allowed.