A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_string_rfind.htm below:

C++ String rfind Function

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 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 <iostream>
#include <string>
#include <cstddef>
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<iostream>
#include<string>
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<iostream>
#include<string>
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<iostream>
#include<string>
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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4