rfind Function

Correct me if I'm wrong but why does the rfind function display 8? Isn't 4 supposed to be displayed? Start counting backwards from 0 to 4? Spam will be found int the sentence "This is spam!".


1
2
3
4
5
      string s2("This is spam!");
    // rfind is the reverse find
    // counts right to left
    cout << s2.rfind("spam") << endl; // Displays 8
    // Starts counting index backwards from 0 
No, 8 is correct.

It returns the position just like find() does (ie: 0 is the first position, not the last).

The difference between find and rfind is tha find will return the first occurance whereas rfind will return the last:

1
2
3
4
string s2("test test");

cout << s2.find("test");  // 0
cout << s2.rfind("test");  // 5 
Topic archived. No new replies allowed.