Thread: How I can search a string and identify where words begin and end?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Question How I can search a string and identify where words begin and end?

    Please, i would like to know how to search a string and identify where words begin and end.

    for example: want to search "how" from the string "Hello how are you?"
    However, I want the index number in the array from where the find starts i.e 7 as "how" starts from 7 position
    in "Hello how are you?" string.


    Thanks a lot
    Last edited by alone2002dj; 12-09-2007 at 08:44 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    There are a number of ways to search a string. Ask a more specific question to receive a more specific answer.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    for example: want to search "how" from the string "Hello how are you?"
    However, I want the index number in the array from where the find starts i.e 7 as "how" starts from 7 position
    in "Hello how are you?" string.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Google for "C String search".

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    Quote Originally Posted by alone2002dj View Post
    for example: want to search "how" from the string "Hello how are you?"
    However, I want the index number in the array from where the find starts i.e 7 as "how" starts from 7 position
    in "Hello how are you?" string.
    To split up strings and find a certain word out of them use strtok.

    Not sure about finding position.
    Code:
    newstring = strtok(oldstring," ");
            while(newstring != NULL)
            {
                printf("%s ",t);
                
                // Get the next token on the line
                newstring = strtok(oldstring," ");
            }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and what is the t in the sample?
    the subsequent calls to strtok should be made with NULL as a first parameter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    you could use strstr() to get a pointer to the first element in the target string, and from that pointer loop until you encounter a whitespace character or null terminator. for example:

    Code:
    char *str = "Hello how are you?", *p;
    
    for(p = strstr(str, "how"); *p && *p != ' '; p++) {
       /* we're in the string "how", do something with p */
    }

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    Quote Originally Posted by vart View Post
    and what is the t in the sample?
    the subsequent calls to strtok should be made with NULL as a first parameter
    t should have been the newstring. I edited the names to make it easier to read and missed that one.

Popular pages Recent additions subscribe to a feed