Thread: find string function

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    find string function

    hi,

    in a program, i currently have:
    Code:
    string line, hey(" hey ");
    getline(cin, line);
    line.find(hey, 0);
    which will as ask for a line of input and will then search the line for the word hey surrounded by a space on each side.

    so far the find function only searches once starting at the begining of the string. how can i modify it to keep finding more than one occurance of " hey " ?

    thanks,
    barneygumble742

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    In general, you can do it like this using the find method...
    Code:
        for (string::size_type pos = 0;
             (pos = line.find(hey, pos)) != string::npos;
             ++pos)
        {
            cout << "found occurrence starting at " << pos << endl;
        }//for
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM