Thread: pattern matching

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Question pattern matching

    I am a little lost i am trying to find all occurrences of a string inside a string.
    If I have for example

    Code:
    string quote = "John loves Suzy. Suzy doesnt love John. Suzy loves Grover. John does not like Grover. "
    and I wanted to find all instances of the word John in the previous quote.
    i think this is close but i am not sure
    Code:
    int position = quote.find("John", 0);
    while (position != string::npos)
    {
    j++
    }
    cout << "The number of times John was used is: " << j++;
    I am desperately trying to learn can anyone help?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    find has an overload that lets you specify at which character in the string to start searching. Each search should be done starting after the previously found position.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Code:
    int position = quote.find("John", position +1);
    Thanks for that catch I still dont know how to use it in a program or driver can anyone help

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    that's a good start, although you need to update the value of position inside your while loop, else it will run forever.

    How about performing find() a second time, starting from position+1...


    edit - a few messages appeared after i hit reply, it looks like you've already got it sussed.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Code:
    int position = quote.find("John", 0);
    while (position != string::npos)
    {
    j++
    }
    cout << "The number of times John was used is: " << j++;
    int position = quote.find("John", position + 1);
    while (position != string::npos)
    {
    j++
    }
    cout << "The number of times John was used is: " << j++;
    thanks for the help im still lost;(

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by gunghomiller View Post
    Code:
    int position = quote.find("John", position +1);
    Thanks for that catch I still dont know how to use it in a program or driver can anyone help
    Unfortunately that won't work, because 'position' is undefined the first time you perform your search.. start out with
    Code:
    int position = 0;

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by gunghomiller View Post
    Code:
    int position = quote.find("John", 0);
    while (position != string::npos)
    {
    j++
    }
    cout << "The number of times John was used is: " << j++;
    int position = quote.find("John", position + 1);
    while (position != string::npos)
    {
    j++
    }
    cout << "The number of times John was used is: " << j++;
    thanks for the help im still lost;(
    Your original problem is still there.. You only need one while loop, but you need to update position inside the body of that loop, not before or after.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    This is what I have so far
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
     int position = 0;
     string quote = "John loves Suzy. Suzy doesnt love John. Suzy loves Grover. John does not like Grover. ";
     int j;
     quote.find("John", 0);
    while (position != string::npos)
    {
    j++;
    }
    cout << "The number of times John was used is: " << j++;
    }
    I am using vb 5.0 it compiles but nothing comes up and it gives me a runtime check the variable j is used without being defined

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, the counter j is uninitialized.

    Secondly, what would change position in the while loop? Hint: you need to put the call to find into the loop somehow.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
     int position = 0;
     int j = 0;
    
    
      string quote = "John loves Suzy. Suzy doesnt love John. Suzy loves Grover. John does not like Grover. ";
      
    while (position != string::npos)
    {
    	j = quote.find("John", 0);
    }
    	
    cout << "The number of times John was used is: " << j;
    }
    I defined j i think however nothing comes up upon building it

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you're not incrementing j anymore. You're setting it to the position in the string of the last "John", which is probably not what you want. Start incrementing it, and use the return value for something else (see lower down).

    You also have an infinite loop. You're searching the same string over and over again for "John". You find "John"; then you start at the beginning of the string and look for it again, and of course you find it once more.

    You have to start searching beyond the position that you just found. Use the return value of find(), and start searching at one past that position in the string next time 'round. (Or maybe four past, one for each letter in "John"; otherwise, upon searching for "gg" in "ggg", you'd find it twice.) You might have to be careful not to go beyond the end of the string here.
    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.

  12. #12
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    j = quote.find("John", 0);
    You specify that the search for the string "John" will always start at 0. You need to replace 0 with a variable. I believe you want that to be the variable position. J will equal the position where "John" is found, so you need to make the variable position equal to J+1. When you loop back to search for the next instance of "John" the search will start there. Lastly you will need a variable to count how many times you found "John".


    Code:
    int foundStringAtPos=string.find("StringToLocate",size_t startSearchFromThisPosition)
    Last edited by manofsteel972; 07-22-2007 at 04:58 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by gunghomiller View Post
    Code:
    while (position != string::npos)
    {
    	j = quote.find("John", 0);
    }
    Analyse this loop for a second.

    You started out with
    Code:
    position = 0
    j = 0

    the first time the loop runs, the condition checks to see whether position is equal to npos
    then it sets 'j' to the position of the the first occurance of "John".

    then the loop condition is checked again, but position is unchanged, and will never change.


    You were very close with this post..
    Quote Originally Posted by gunghomiller View Post
    Code:
    int position = quote.find("John", position +1);
    Thanks for that catch I still dont know how to use it in a program or driver can anyone help
    If you get rid of the 'int' at the front of that line, that will update your position variable every time the loop runs.
    Last edited by Bench82; 07-22-2007 at 04:54 PM.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    I changed it to
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
     int position = 0;
     int j = 0;
    
    
      string quote = "John loves Suzy. Suzy doesnt love John. Suzy loves Grover. John does not like Grover. ";
      
    while (position != string::npos)
    {
    	position = quote.find("John", position +1);
    	cout << "The number of times John was used is: " << j;
    }
    
    }
    and it displays "The number of times John was used is:0" three times. Which it used three times however i dont want the sentence to be displayed three times i want the number three to be displayed. I appreciate all the help immensly gents but the terms I do not know all of them.

  15. #15
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You don't want to print until after you are done looping. Move that print satement below that loop, and replace it with j++;
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. filename pattern matching
    By dwks in forum C Programming
    Replies: 11
    Last Post: 07-13-2005, 10:23 AM
  3. Pattern matching
    By GSLR in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-06-2003, 07:17 AM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. pattern matching
    By ahahplz in forum C Programming
    Replies: 5
    Last Post: 02-07-2003, 07:15 PM