C++ String::rfind() function



The C++ std::string::rfind() function is used to locate the last occurrence of a specified substring or character within a string. It searches from the end of the string towards the beginning, providing a reverse search capability. It returns the position of the substring or character if found, otherwise it returns std::string::npos.

Syntax

Following is the syntax for std::string::rfind() function.

size_t rfind (const string& str, size_t pos = npos) const noexcept;
or	
size_t rfind (const char* s, size_t pos = npos) const;
or
size_t rfind (const char* s, size_t pos, size_t n) const;
or
size_t rfind (char c, size_t pos = npos) const noexcept;

Parameters

  • str − It indicates the another string object.
  • pos − It indicates the position of the last character in the string to be considered in the search.
  • s − It indicates the pointer to an array of characters.
  • n − It indicates the length of sequence of characters to match.
  • c − It indicates the individual character to be searched for.

Return value

This function returns the position of the first charcter of last match.

Example 1

Following is an example to find the std::string::find using C++.

#include 
#include 
#include 
int main() {
   std::string str("sairamkrishna mammahe is a one of the tech person in tutorialspoint.com");
   std::string key("mammahe");
   std::size_t found = str.rfind(key);
   if (found != std::string::npos)
      str.replace(found, key.length(), "tech");
   std::cout << str << '\n';
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

sairamkrishna tech is a one of the tech person in tutorialspoint.com   

Example 2

In the below program, we have initialized string x = "Tutorialspoint is a educate company!" and given the position to search = "educate" using string::rfind() function. So it prints position of that particular first character of last match of that word.

#include
#include
using namespace std;
int main() {
   string x = "Tutorialspoint is educate company!";
   string position = "educate";
   int i = x.rfind(position);
   cout << i;
   return 0;
}

Output

If we run the above code it will generate the following output.

18

Example 3

In below program we have initialized string x and we are declared finding a single chararacter in the string by using string::rfind() function.

#include
#include
using namespace std;
int main() {
   string x = "Tutorialspoint";
   int i = x.rfind('a');
   cout << i;
   return 0;
}

Output

Following is the output of the above code.

6                            

Example 4

In the below program we have initialized string x and passing the position as the parameter by giving x.find() using string::find() function.

#include
#include
using namespace std;
int main() {
   string x = "Computer Science Engineering";
   int i = x.rfind("Science", 10);
   cout << i;
   return 0;
}  

Output

Following is the output of the above code.

9         
string.htm
Advertisements