std::string str = "hello world";
size_t pos = str.rfind("o");
if (pos != std::string::npos) {
std::cout << "Last occurrence of 'o' is at position " << pos << std::endl;
}
std::string str = "hello world";
size_t pos = str.rfind("o", 5);
if (pos != std::string::npos) {
std::cout << "Last occurrence of 'o' before position 5 is at position " << pos << std::endl;
}
std::string str = "hello world";
size_t pos = str.rfind("world");
if (pos != std::string::npos) {
std::string sub = str.substr(pos, 5);
std::cout << "Substring found: " << sub << std::endl;
}
std::string str = "hello world hello";
size_t pos = str.rfind("hello");
while (pos != std::string::npos) {
std::cout << "Last occurrence of 'hello' is at position " << pos << std::endl;
pos = str.rfind("hello", pos - 1);
}