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

This is a discussion on How I can search a string and identify where words begin and end? within the C Programming forums, part of the General Programming Boards category; Please, i would like to know how to search a string and identify where words begin and end. for example: ...

  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 07:44 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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,211
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    and what is the t in the sample?
    the subsequent calls to strtok should be made with NULL as a first parameter
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21