prefix text with spaces

This is a discussion on prefix text with spaces within the C Programming forums, part of the General Programming Boards category; I need to prefix a few lines of text with either 3 or 6 spaces by just using printf statement. ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    prefix text with spaces

    I need to prefix a few lines of text with either 3 or 6 spaces by just using printf statement. The lines vary in lenght. One of the lines is 10 characters and needs 3 spaces before it, thus I used

    printf ("%13s\n", "10chartext");

    In this case I counted the lenght of the text. Is there another efficient way to achieve this to prefix the some longer text with 6 spaces sing just printf?


    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,921
    It depends on what you mean by "just using printf statement". For example, I would expect that you may want to use strlen to find out the length of the string to determine the field width. Then, you might use sprintf to format the format string for use with printf

    How do you know when to prefix by 3 and when to prefix by 6 spaces anyway?
    Last edited by laserlight; 10-03-2011 at 09:54 PM. Reason: Second use of sprintf should have been printf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Laserlight
    I meant to say, I should be using only printf statements. The program is part of a beginning course. Of the total 6 lines, 1st, 3rd and 6th lines are to be prefixed by 3 and rest to be prefixed by 6 spaces.
    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like you should check what line you are on and print extra spaces accordingly.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,921
    In that case, what you are doing is fine. Count the number of characters of the string yourself and set the field with to be +3 (or +6) of what you counted.

    EDIT:
    Quote Originally Posted by quzah
    Sounds like you should check what line you are on and print extra spaces accordingly.
    Yeah, this could be less tedious, i.e., do not set the field width at all.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Of course you do know you can do this...

    Code:
    printf("%s%s","   ",str);
    ... right?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then of course there is:
    Code:
    printf( "%*s", width, buf );

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by wearld97 View Post
    printf ("%13s\n", "10chartext");
    Do you mean:
    Code:
      printf("%s%s\n","             "+strlen("hellow"),"hellow");
      printf("%s%s\n","             "+strlen("hellowo"),"hellowo");
      printf("%s%s\n","             "+strlen("hellowor"),"hellowor");
      printf("%s%s\n","             "+strlen("helloworl"),"helloworl");
      printf("%s%s\n","             "+strlen("helloworld"),"helloworld");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning count of spaces in a text
    By |HBO| in forum C Programming
    Replies: 5
    Last Post: 05-23-2010, 07:30 PM
  2. Reading Text File (spaces problem)
    By kekewong in forum C Programming
    Replies: 1
    Last Post: 04-15-2009, 03:34 PM
  3. Replies: 1
    Last Post: 03-08-2005, 11:02 AM
  4. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 04:18 AM
  5. Reading in text file into an array, white spaces...
    By error in forum C++ Programming
    Replies: 12
    Last Post: 01-14-2003, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21