#include#include using namespace std; int main() { string str = "Hello World"; char c = 'o'; // find the position of the last occurrence of 'o' in the string size_t pos = str.reverseFind(c); cout << "Last occurrence of " << c << " at position " << pos << endl; // output: Last occurrence of o at position 7 return 0; }
#includeIn both examples, the reverseFind function is used to search for a specified character or sub-string within a string. The returned position of the last occurrence is then printed to the console. The cpp String package library is used to perform these operations.#include using namespace std; int main() { string str = "The quick brown fox jumps over the lazy dog"; string subStr = "fox"; // find the position of the last occurrence of sub-string "fox" in the string size_t pos = str.reverseFind(subStr); cout << "Last occurrence of \"" << subStr << "\" at position " << pos << endl; // output: Last occurrence of "fox" at position 16 return 0; }