The std::string::rfind() function is a member of the std::string class defined in the <string> header. It searches a string in the reverse direction to locate the last occurrence of a character or substring.
- Supports searching for both characters and substrings.
- Returns the index of the match or std::string::npos if no match is found.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Welcome to GeeksforGeeks";
cout << str.rfind('e');
return 0;
}
Output
21
Explanation: The last occurrence of 'e' is found at index 21, so rfind() returns 21.
Syntax
string_name.rfind(character); //Search for a character
string_name.rfind(substring); //Search for a substring
string_name.rfind(character, position); //Search before a given position
Parameters
The function accepts the following parameters:
- character - Character to search.
- substring - String to search.
- position (optional) - Position before which the search is performed.
Return Value
The function returns:
- Index of the last occurrence if found.
- std::string::npos if no match exists.
Complexity Analysis
| Operation | Time Complexity | Auxiliary Space |
|---|---|---|
| rfind() | O(n × m) (worst case) | O(1) |
where:
- n = length of source string
- m = length of substring
Searching for a single character is typically O(n).
Examples
The following examples demonstrate different uses of std::string::rfind().
1. Find the Last Occurrence of a Character
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Welcome to GeeksforGeeks";
size_t pos = str.rfind('e');
if (pos != string::npos)
cout << pos;
return 0;
}
Output
21
Explanation: rfind('e') searches from right to left and returns the index of the last 'e'.
2. Find the Last Occurrence of a Substring
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Welcome to GeeksforGeeks";
size_t pos = str.rfind("to");
if (pos != string::npos)
cout << pos;
return 0;
}
Output
8
Explanation: The substring "to" appears once, starting at index 8.
3. Search Before a Given Position
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Welcome to GeeksforGeeks";
size_t pos = str.rfind('e', 5);
if (pos != string::npos)
cout << pos;
return 0;
}
Output
1
Explanation: The search is restricted to characters before index 5, so the last matching 'e' is found at index 1.
4. Handling a Failed Search
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Welcome to GeeksforGeeks";
size_t pos = str.rfind("Java");
if (pos == string::npos)
cout << "Substring not found";
return 0;
}
Output
Substring not found
Explanation: Since "Java" does not exist in the string, rfind() returns string::npos.
Best Practices
Follow these practices while using std::string::rfind():
- Always compare the result with std::string::npos before using the returned index.
- Use the position parameter when searching within a specific portion of the string.
- Prefer rfind() over manual reverse traversal for better readability.
- Use find() instead if the first occurrence is required.
Comparison with Similar Functions
| Function | Description |
|---|---|
| find() | Searches from the beginning of the string. |
| find_first_of() | Finds the first occurrence of any character from a set. |
| find_last_of() | Finds the last occurrence of any character from a set. |