String Rfind Function - C++ Programming Tutorial
C++ Course / Strings / String Rfind Function

String Rfind Function

BLUF: Mastering String Rfind Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Rfind Function

C++ is renowned for its efficiency. Learn how String Rfind Function enables low-level control and high-performance computing in the tutorial below.

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:

Example

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.

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.

Example

#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.

Example

#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.

Example

#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:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience