The second parameter of the find
function sets the offset against the text. That is, s.find("tigre", 0)
will search for the first occurrence of the word "tiger" from position 0 of s
, already s.find("tigre", 10)
will search for the first occurrence of the word from position 10 of s
. What the program does is update the value of this offset 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
is 0, so:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 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, run loop . The new value of p
will be the return of the position where the word tigre
is found in the text, with a p
offset. Since p
is zero, the function will search from position 0 of the text, locating the word in 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 displaying on 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 find
function will look for the word in the text from position p
, which is 4, so it will no longer consider the first occurrence of the word. The new value of p
, in this case, will 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
At the next iteration, the find
function will try to locate the word tigre
in the original text, but only from position 16, ignoring the first two occurrences. This way, 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
Once displayed, 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 find
function will search for the word tigre
from position 29. Since there are no more occurrences of the word, -1 is returned, so p
will be -1, the while condition is undone and the program terminates.