How would i align to the left using strings in C?

C automatically aligns strings to the right...

Page 1 of 1

4 Replies - 7334 Views - Last Post: 16 May 2010 - 07:34 PM Rate Topic: -----

#1 Cloudyeyes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 15-May 10

How would i align to the left using strings in C?

Posted 16 May 2010 - 06:46 PM

Whenever I type in a bit of code to print a string out, it'll automatically align to the right. However, I want the strings to align to the left.

For example, if I do printf("%s", stringvalue); //with stringvalue = abcdef, with length 9, etc...
It'll align abcdef to the right.

How do i change it so it aligns to the left?
Is This A Good Question/Topic? 0
  • +

Replies To: How would i align to the left using strings in C?

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:07 PM

View PostCloudyeyes, on 16 May 2010 - 07:46 PM, said:

Whenever I type in a bit of code to print a string out, it'll automatically align to the right. However, I want the strings to align to the left.

For example, if I do printf("%s", stringvalue); //with stringvalue = abcdef, with length 9, etc...
It'll align abcdef to the right.

How do i change it so it aligns to the left?


Try searching under C strings left align

Hint: 4 down in my search was this ...

- Causes printf to left-align the output of this placeholder (the default is to right-align the output).

This post has been edited by David W: 16 May 2010 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:11 PM

#include <stdio.h>

int main()
{

  printf ("%10s\n",  "Right");

  printf ("%-10s\n", "Left");

  printf ("%-10s\n", "   abcdef");

  printf ("%-10s\n", "abcdef   ");

  getchar();

  return (0);
}



Output.
     Right
Left      
   abcdef 
abcdef    


Was This Post Helpful? 2
  • +
  • -

#4 Cloudyeyes  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 15-May 10

Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:27 PM

View Post#define, on 16 May 2010 - 06:11 PM, said:

#include <stdio.h>

int main()
{

  printf ("%10s\n",  "Right");

  printf ("%-10s\n", "Left");

  printf ("%-10s\n", "   abcdef");

  printf ("%-10s\n", "abcdef   ");

  getchar();

  return (0);
}



Output.
     Right
Left      
   abcdef 
abcdef    




Thanks! Got it working! Exactly what I was looking for. :)
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: How would i align to the left using strings in C?

Posted 16 May 2010 - 07:34 PM

View PostCloudyeyes, on 17 May 2010 - 03:27 AM, said:

Thanks! Got it working! Exactly what I was looking for. :)


Cool :) B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1