std::string::rfind in C++ with Examples
The std::string::rfind is a string class member function that is used to search the last occurrence of any character in the string. If the character is present in the string then it returns the index of the last occurrence of that character in the string else it will return string::npos which denotes the pointer is at the end of the string. Header File:
#include < string >
Syntax 1:
rfind(char ch)
rfind(string str)
Parameters: This function takes a given character or a string as a parameter, whose index is to be found. Return value: This method returns the position of the last occurrence of that character or first index of the last occurrence of the string. Program 1: Below is the program to illustrate string::rfind(char ch):
CPP
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
void findLastOccurrence(string str, char ch)
{
size_t found;
found = str.rfind(ch);
if (found == string::npos) {
cout << "Character " << ch
<< " is not present in"
<< " the given string.";
}
else {
cout << "The last occurrence of '"
<< ch << "' is found at index: "
<< found << endl;
}
}
int main()
{
string str("Welcome to GeeksforGeeks!");
char ch = 'e' ;
findLastOccurrence(str, ch);
}
|
Output:The last occurrence of ‘e’ is found at index: 21
Program 2: Below is the program to illustrate string::rfind(string str):
CPP
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
void findLastOccurrence(string str, string s)
{
size_t found;
found = str.rfind(s);
if (found == string::npos) {
cout << "String '" << s
<< "' is not present in"
<< " the given string.";
}
else {
cout << "The first index of last "
<< "occurrence of '" << s
<< "' is found at index: "
<< found << endl;
}
}
int main()
{
string str("Welcome to GeeksforGeeks!");
string s = "to";
findLastOccurrence(str, s);
}
|
Output:The first index of last occurrence of ‘to’ is found at index: 8
Syntax 2:
rfind(char ch, size_t position);
rfind(string s, size_t position);
Parameters: This function takes:
- a given character or a string as a parameter, whose index is to be found.
- a position till where the search is to be performed.
Return value: This method returns the position of the first character of the last match of that given character or string before that position else it returns string::npos Program 3: Below is the program to illustrate string::rfind(char ch, size_t position):
CPP
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
void findLastOccurrence(
string str, char ch, size_t position)
{
size_t found;
found = str.rfind(ch, position);
if (found == string::npos) {
cout << "Character " << ch
<< " is not present in"
<< " the given string.";
}
else {
cout << "The last occurrence of "
<< ch << " before position "
<< position
<< " is found at index: "
<< found << endl;
}
}
int main()
{
string str("Welcome to GeeksforGeeks!");
char ch = 'e' ;
size_t position = 5;
findLastOccurrence(str, ch, position);
}
|
Output:The last occurrence of e before position 5 is found at index: 1
Program 4: Below is the program to illustrate string::rfind(string str, size_t position):
CPP
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
void findLastOccurrence(
string str, string s, size_t position)
{
size_t found;
found = str.rfind(s, position);
if (found == string::npos) {
cout << "String " << s
<< " is not present in"
<< " the given string.";
}
else {
cout << "The last occurrence of "
<< s << " before position "
<< position
<< " is found at index: "
<< found << endl;
}
}
int main()
{
string str("Welcome to GeeksforGeeks!");
string s = "to";
size_t position = 5;
findLastOccurrence(str, s, position);
}
|
Output:String to is not present in the given string.
Last Updated :
26 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...