How to reverse find Substring in C++ string

In this article, we are going to learn How to find Substring in C++ string from the reverse with the help of multiple ways. We are going to use the STL rfind() algorithms of the string container class. This function helps is search a string in reverse order for a substring Also we are going to learn how to find the last occurrence of a character from the beginning or you can say the first occurrence from the end. So let us start ways to find Substring in a C++ String.

STL rfind() function in C++


STL string class provides us rfind() function that can be used to Find Substring in a String in C++ from the reverse direction. Also, you can find a character in a string occurring from the end. So let us understand the rfind() function and how it can be used in different flavors.

Syntax rfind() function:

rfind(string str)
rfind(char ch)

Parameters

  • It takes a character or a string as a parameter, whose index is to be found.

Return value

It returns the position of the last occurrence of that character or the first index of the last occurrence of the string. if it is not found then it will return string::npos which denotes the pointer is at the end of the string.

1. STL rfind() function to Find Substring in a String in C++


In this example, we are going to see the first flavors of rfind() function where we are searching a substring in a string.
In this example, this search is going to be in reverse order means the search will happen from the last index to the first index.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str1 = "This is Devenum.com string tutorial.";
    string str2 = "string";
    string str3 = "list";

    if(str1.rfind(str2) != string::npos)
	{
        cout << "String1 contains the String 2 as a substring and exist at index " << str1.rfind(str2)<< endl ;
	}
	else
        cout << "String1 Does Not contains the String 2 as a substring" << endl;

    if(str1.rfind(str3) != string::npos)
	{
        cout << "String1 contains the String 3 as a substring and exist at index" << str1.rfind(str2) << endl;
	}
	else
        cout << "String1 Does Not contains the String 3 as a substring" << endl;

    return 0;
}

Output

String1 contains the String 2 as a substring and exist at index 20
String1 Does Not contains the String 3 as a substring

2. STL rfind() function to Find character in a String in C++


In this example, we are going to learn how to use rfind() function to search for a character in a string.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string str1 = "This string tutorial is on Devenum.com ";
    char ch2 = 's';
	char ch3 = 'p';

    if(str1.rfind(ch2) != string::npos)
	{
        cout <<" We found the Character in the string " << endl;
	}
	else
        cout << "We didnt find the Character in the string" << endl;
		
	if(str1.rfind(ch3) != string::npos)
	{
        cout <<" We found found the Character in the string " << endl;
	}
	else
        cout << "We didnt find the Character in the string" << endl;

    return 0;
}

Output

We found the Character in the string
We didnt find the Character in the string