Thread: Printing part of a local variable

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    Printing part of a local variable

    Hello,

    I was wondering if there is a way to only get a certain part of text from a variable. For example if y = hello hey sup howdy, is there any easy way to choose and print one of those varialbes? Thanks!

  2. #2
    kiddo
    Join Date
    Sep 2005
    Posts
    8
    i dint get u. can u elaborate on it.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    ok,

    I have a varialbe that is the following:

    y = -1 hello hey sup -2 bye cya

    Is there a way that I can get it to print everything except everything after -2?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Sure, you can use strstr() to find the bits of strings which interest you.

    Code:
    char test[] = "this is a message";
    char *p = strstr( test, "mess" );
    if ( p != NULL ) {
      len = p - test;
      printf( "%.*s\n", len, test );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    Thats almost what I need, Salem. But how would I want to include everything [I]after[I] "message"?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    printf("%s",p) ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Probably more like
    Code:
    printf("%s", p+strlen("message "));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by dwks
    Probably more like
    Code:
    printf("%s", p+strlen("message "));
    may be this,just a matter of space .
    printf("%s", p+strlen("message"));

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, since your way would print a space in front of p.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    c compiler ignores white spaces like , p.but strlen will give 1 more character for space in string.so i think ur will eat a character after message

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    c compiler ignores white spaces . . .
    It does, but not in strings. Check a binary dump if you like (or use gcc -S).
    strlen will give 1 more character for space in string.so i think ur will eat a character after message
    Not if the message is
    Code:
    "this is the message and a long one at that"
    Then your code would produce a pointer to a string that looks like this:
    Code:
    " and a long one at that"
    Wheras mine would point to
    Code:
    "and a long one at that"
    Do you see?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. Replies: 20
    Last Post: 11-12-2005, 03:10 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. printing selected bytes of an int variable
    By earth_angel in forum C++ Programming
    Replies: 18
    Last Post: 07-14-2005, 06:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM