std::string::rfind in C++ with Examples

The std::string::rfind is a string class member function that is used to search the last occurrence of any character in the string. If the character is present in the string then it returns the index of the last occurrence of that character in the string else it will return string::npos which denotes the pointer is at the end of the string.

Header File:

#include < string >

Syntax 1:

rfind(char ch)
rfind(string str)

Parameters: This function takes a given character or a string as a parameter, whose index is to be found.

Return value: This method returns the position of the last occurrence of that character or first index of the last occurrence of the string.



Program 1:

Below is the program to illustrate string::rfind(char ch):

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// rfind() method
  
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
  
// Function to return last occurrence
// of character in a string
void findLastOccurernce(string str, char ch)
{
  
    // To store the index of the result
    size_t found;
  
    // Function to find the last
    // occurence of character ch
    // in string str
    found = str.rfind(ch);
  
    // If string doesn't have
    // character ch present in it
    if (found == string::npos) {
        cout << "Character " << ch
             << " is not present in"
             << " the given string.";
    }
  
    // Else print the position
    else {
        cout << "The last occurence of '"
             << ch << "' is found at index: "
             << found << endl;
    }
}
  
// Driver Code
int main()
{
    // Given String
    string str("Welcome to GeeksforGeeks!");
  
    // Character to be found
    char ch = 'e';
  
    findLastOccurernce(str, ch);
}

chevron_right


Output:

The last occurence of ‘e’ is found at index: 21

Program 2:

Below is the program to illustrate string::rfind(string str):

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// rfind() method
  
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
  
// Function to return last occurrence
// of string in a string
void findLastOccurernce(string str, string s)
{
  
    // To store the index of the result
    size_t found;
  
    // Function to find the first index
    // of last occurence of string s in str
    found = str.rfind(s);
  
    // If string doesn't have
    // string s present in it
    if (found == string::npos) {
        cout << "String '" << s
             << "' is not present in"
             << " the given string.";
    }
  
    // Else print the position
    else {
        cout << "The first index of last "
             << "occurence of '" << s
             << "' is found at index: "
             << found << endl;
    }
}
  
// Driver Code
int main()
{
    // Given String
    string str("Welcome to GeeksforGeeks!");
  
    // string to be found
    string s = "to";
  
    findLastOccurernce(str, s);
}

chevron_right


Output:

The first index of last occurence of ‘to’ is found at index: 8



Syntax 2:

rfind(char ch, size_t position);
rfind(string s, size_t position); 

Parameters: This function takes:

  • a given character or a string as a parameter, whose index is to be found.
  • a position till where the search is to be performed.

Return value: This method returns the position of the first character of the last match of that given character or string before that position else it returns string::npos

Program 3:

Below is the program to illustrate string::rfind(char ch, size_t position):

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the function
// string::rfind(char ch, size_t pos)
  
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
  
// Function to return last occurrence
// of character in a string
void findLastOccurernce(
    string str, char ch, size_t position)
{
  
    // To store the index of the result
    size_t found;
  
    // Function to find the last occurence
    // of character ch in str before pos 5
    found = str.rfind(ch, position);
  
    // If string doesn't have
    // character ch present in it
    if (found == string::npos) {
        cout << "Character " << ch
             << " is not present in"
             << " the given string.";
    }
  
    // Else print the position
    else {
        cout << "The last occurence of "
             << ch << " before position "
             << position
             << " is found at index: "
             << found << endl;
    }
}
  
// Driver Code
int main()
{
    // Given String
    string str("Welcome to GeeksforGeeks!");
  
    // Character to be found
    char ch = 'e';
  
    // Position till where
    // the search is to be done
    size_t position = 5;
  
    findLastOccurernce(str, ch, position);
}

chevron_right


Output:

The last occurence of e before position 5 is found at index: 1

Program 4:

Below is the program to illustrate string::rfind(string str, size_t position):

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the function
// string::rfind(string str, size_t pos)
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
  
// Function to return last occurrence
// of string in a string
void findLastOccurernce(
    string str, string s, size_t position)
{
  
    // To store the index of result
    size_t found;
  
    // Function to find the last occurence of
    // string s in str before given position
    found = str.rfind(s, position);
  
    // If string doesn't have
    // string s present in it
  
    // If string doesn't have
    // string s present in it
    if (found == string::npos) {
        cout << "String " << s
             << " is not present in"
             << " the given string.";
    }
  
    // Else print the position
    else {
        cout << "The last occurence of "
             << s << " before position "
             << position
             << " is found at index: "
             << found << endl;
    }
}
  
// Driver Code
int main()
{
  
    // Given String
    string str("Welcome to GeeksforGeeks!");
  
    // string to be found
    string s = "to";
  
    // Position till where
    // the search is to be done
    size_t position = 5;
  
    findLastOccurernce(str, s, position);
}

chevron_right


Output:

String to is not present in the given string.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.