The second function parameter find
sets the offset relative to the text. This is, s.find("tigre", 0)
will seek the first occurrence of the word "tiger" from position 0 of s
, already s.find("tigre", 10)
seek the first occurrence of the word from position 10 of s
. What the program does is update the value of this shift so that the same occurrence of the word is not considered more than once.
TL;DR
Our text:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 ...
The value of p
initial is 0, therefore:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 ...
^
p
While p
is greater than -1, execute the loop. The new value of p
will be the return of the position where the word is found tigre
in the text, with an offset of p
. Like p
is zero, the function will search from position 0 of the text, locating the word at position 3. Then, the new value of p
will be 3.
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 ...
^
p
After displayed on the screen, p
is incremented by 1:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 ...
^
p
In the next iteration, the function find
will search for the word in the text from the position p
, which is worth 4, so he will not consider more the first occurrence of the word. The new value of p
, in this case, it shall be 15.
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
^
p
Again, when displayed on the screen, the value of p
is incremented by 1.
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
^
p
In the next iteration, the function find
will attempt to locate the word tigre
in the original text, but only from position 16, disregarding the first two occurrences. Thus, the new value of p
will be 28.
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
^
p
After displayed on screen, again the value of p
is incremented by 1:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| u | m | | t | i | g | r | e | , | | d | o | i | s | | t | i | g | r | e | s | , | | t | r | ê | s | | t | i | g | r | e | s |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
^
p
Finally, in the last iteration, the function find
will seek by word tigre
from position 29. Since there are no more occurrences of the word, it is returned -1, therefore p
will be worth -1, causing the condition of the while is undone and the program is terminated.
Bah, very good.
– Jéf Bueno
what a mood! congratulations =]
– Rovann Linhalis
Dude, you MYTHED on the answer, meme Thug life for you, hahaha
– Eduardo