Comparing strings from within strings

Discussion in 'C Programming' started by Rick, Oct 21, 2003.

  1. Rick

    Rick Guest

    Hi,

    I need to extract some data from a string being passed. I was wondering
    what would be the best way of doing this. For example, I have two
    strings: "SWITCH1CLOSE" and "GATE1OPEN"

    I need to take out the SWITCH part of out of the string, the "1" and the
    "close". Similarly, I need to take out GATE, 1 and open from the second
    string. Should I use the "strstr" function for comparing the substrings
    GATE, CLOSE, OPEN etc? I can't use the tokenizing function here so I'm
    not sure what would be the best way of doing this. Any suggestions would
    be great! Thanks


    Rick
    Rick, Oct 21, 2003
    #1
    1. Advertising

  2. Rick wrote:

    > Hi,
    >
    > I need to extract some data from a string being passed. I was wondering
    > what would be the best way of doing this. For example, I have two
    > strings: "SWITCH1CLOSE" and "GATE1OPEN"
    >
    > I need to take out the SWITCH part of out of the string, the "1" and the
    > "close". Similarly, I need to take out GATE, 1 and open from the second
    > string. Should I use the "strstr" function for comparing the substrings
    > GATE, CLOSE, OPEN etc? I can't use the tokenizing function here so I'm
    > not sure what would be the best way of doing this. Any suggestions would
    > be great! Thanks


    Yes, strstr is fine. You might not want to search from the beginning each
    time, though. For example, you might want to find "GATE" and "OPEN" in your
    second example. You can use a pointer to keep track of how far "into" the
    string you are, and strstr to compare from that point.

    char s[] = "SOMERANDOMLEADINGTEXTGATE1OPEN";
    char *p = strstr(s, "GATE");

    if(p != NULL)
    {
    /* do whatever it was you needed to do at this point, then ... */
    p += strlen("GATE"); /* skip past characters of "GATE", so p now points to
    the '1' character. */
    p = strstr(p, "OPEN"); /* off to the next match... */


    If you want to jump forward to a particular /character/, you might also find
    strchr useful.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 21, 2003
    #2
    1. Advertising

  3. Rick

    Rick Guest

    Thanks a lot Richard! One question though, is there a Standard C
    function that converts lower case characters to upper case? Thanks

    Rick
    Rick, Oct 21, 2003
    #3
  4. Rick <rrquick@nospam-com> wrote:

    >Thanks a lot Richard! One question though, is there a Standard C
    >function that converts lower case characters to upper case? Thanks


    Lookup toupper in your library reference.

    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Oct 21, 2003
    #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. kaeli
    Replies:
    8
    Views:
    576
    Chris Smith
    Nov 18, 2004
  2. HS1

    Comparing two strings

    HS1, Nov 28, 2004, in forum: Java
    Replies:
    3
    Views:
    440
    Rob van der Leek
    Nov 29, 2004
  3. manzur

    comparing strings

    manzur, Mar 7, 2006, in forum: Java
    Replies:
    8
    Views:
    370
    Roedy Green
    Mar 7, 2006
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    724
    Malcolm
    Jun 24, 2006
  5. Red
    Replies:
    2
    Views:
    1,202
    Pavel Lepin
    May 9, 2007
Loading...

Share This Page