C++ String rfind
This function is utilized to locate the string representing the final instance of the sequence defined by its parameters.
Syntax
Consider a string 'str' and key string 's'. The syntax should be:
str.rfind(s);
str.rfind(s,pos);
str.rfind(s,pos,n);
str.rfind(ch);
Parameter
The str data type represents a sequence of characters and is commonly employed for performing search operations.
It specifies the index of the final character where the search should begin.
n : The quantity of characters to be taken into account during the search
ch : Character value to be searched for.
Example 1
Let's see this simple example.
#include<iostream>
using namespace std;
int main()
{
string str="This is an object oriented programming language";
string key="language";
int i=str.rfind(key);
cout<<i;
return 0;
}
Output:
Example 2
Let's examine another straightforward example by passing a character value.
#include<iostream>
using namespace std;
int main()
{
string str="Computer Science";
int i=str.rfind('e');
cout<<i;
return 0;
}
Output:
Example 3
Let's consider this scenario where the parameter includes a reference to the position pos.
#include<iostream>
using namespace std;
int main()
{
string str="Digital electronics is a B.tech subject";
int i=str.rfind("is",21);
cout<<i;
return 0;
}
Output:
Example 4
Let's examine this instance where the count of characters to match is determined by its arguments.
#include<iostream>
using namespace std;
int main()
{
string str="Java is an object oriented programming language";
int i=str.rfind("programming",40,7);
cout<<i;
return 0;
}
Output: