help

With the find() find the first character.
with the rfind() the last.

what will use so to find all the same characters?

i mean if in one word there are three same words,what i will use to to find and the three letters.
As far as find() goes:

You'll see here: http://www.cplusplus.com/reference/string/string/find/
that find() returns the position (pos) of the first occurrence of the char or string u searched for. All u have to do is save that return value, call find() again and use that returned pos as the start of the string.

Another options (especially if the string is rather long) is creating a new string that starts *at* the position+1 of the first char encountered then calling find() on the new string.

Last edited on
what you mean man plz show me.
for example i have this:

if(takeword.find(guess))
{
//code
}

how will call it again and again?
Could call it recursively until you've searched the entire string
something like this maybe?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    std::string str = "she sells sea shells on the sea shore";
    std::string sea = "sea";
    int pos = 1;

    for(int i=0; i<str.length(); i+=pos)
    {
        pos = int(str.find(sea, i));
        std::cout << sea << " found at: " << pos << std::endl;
        pos++;
    }
}
That's clever badAlloc :p
pos = str.find(sea, i); //don't need int() and pos = 0; //line 8 - not that it would effect the result...
Last edited on
Topic archived. No new replies allowed.