Thread: prefix text with spaces

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:02 PM
  4. Reading spaces, carrage returns, eof from text files
    By thenrkst in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 05: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, 09:39 AM