How to get string from front of array to position x?

Discussion in 'C Programming' started by Angus, Jan 22, 2011.

  1. Angus

    Angus Guest

    Hi

    If I want to eg print a string from position 3 to the end of the
    string I can do this:
    char szMessage[] = "Hello World";
    printf("string fragment: %s\n", szMessage+5);

    But if I just want to print "Hello" how can I do it. I can make a
    copy but what other way to do this?

    Angus
     
    Angus, Jan 22, 2011
    #1
    1. Advertising

  2. Angus

    Willem Guest

    Angus wrote:
    ) If I want to eg print a string from position 3 to the end of the
    ) string I can do this:
    ) char szMessage[] = "Hello World";
    ) printf("string fragment: %s\n", szMessage+5);
    )
    ) But if I just want to print "Hello" how can I do it. I can make a
    ) copy but what other way to do this?

    The width/precision specifiers for the printf formats work on strings
    as well. (Note that there are two specifiers, separated by a dot.)
    As a bonus, you can use * for a specifier, and then give the value
    in the argument list.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jan 22, 2011
    #2
    1. Advertising

  3. Angus

    Eric Sosman Guest

    On 1/22/2011 2:02 PM, Angus wrote:
    > Hi
    >
    > If I want to eg print a string from position 3 to the end of the
    > string I can do this:
    > char szMessage[] = "Hello World";
    > printf("string fragment: %s\n", szMessage+5);


    That would print from position 5 (the ' ') onward, not position 3.

    > But if I just want to print "Hello" how can I do it. I can make a
    > copy but what other way to do this?


    One way is to use the "precision" modifier in the format:

    printf ("string fragment: %.5s\n", szMessage);

    The ".5" instructs printf() to print no more than 5 characters,
    even if the argument string is longer. (If it's shorter, printf()
    will print the entire string.)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 22, 2011
    #3
  4. Angus

    Guest

    Angus <> wrote:
    >
    > If I want to eg print a string from position 3 to the end of the
    > string I can do this:
    > char szMessage[] = "Hello World";
    > printf("string fragment: %s\n", szMessage+5);


    Actually, that's position 5 (or 6, depending on how you count).

    > But if I just want to print "Hello" how can I do it. I can make a
    > copy but what other way to do this?


    printf("string fragment: %.5s\n", szMessage);

    Don't you have a reference that includes printf format specifiers?
    --
    Larry Jones

    From now on, I'm devoting myself to the cultivation of
    interpersonal relationships. -- Calvin
     
    , Jan 22, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    669
    Luqman
    Feb 7, 2006
  2. Alex Martinoff
    Replies:
    4
    Views:
    5,953
    Alex Martinoff
    Sep 6, 2003
  3. Roger
    Replies:
    1
    Views:
    308
    Larry Bates
    Mar 2, 2004
  4. Chetan Gadgil
    Replies:
    1
    Views:
    336
    Roger
    Mar 4, 2004
  5. Payton Swick

    move to front of array

    Payton Swick, Dec 21, 2005, in forum: Ruby
    Replies:
    16
    Views:
    394
    Devin Mullins
    Dec 23, 2005
Loading...

Share This Page