std::string::find_last_of in C++ with Examples
Last Updated :
12 Jul, 2025
The std::string::find_last_of is a string class member function which is used to find the index of last occurrence of any characters in a 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 returns string::npos.
Header File:
#include < string >
Template Class
template < class T >
size_type
find_last_of(const T& t,
size_type pos = npos ) const noexcept();
Syntax 1:
find_last_of(char ch)
Parameters: This function takes a given character and returns the position of the last occurrence of that character.
Below is the program to illustrate string::find_last_of():
CPP
// C++ program to illustrate string::find_last_of
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// Character to be found
char ch = 'e';
// To store the index of last
// character found
size_t found;
// Function to find the last
// character ch in str
found = str.find_last_of(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 last position
// of the character
else {
cout << "Character " << ch
<< " is found at index: "
<< found << endl;
}
}
Output: Character e is found at index: 21
Time Complexity: O(N), here N is the length of the given string and time complexity string::find_last_of() is O(N).
Auxiliary Space: O(1), since we not used any extra space.
Syntax 2:
find_last_of(char ch, size_t position)
Parameters: This function takes a given character and an index till where the search is to be performed. It returns the position of the last occurrence of that character.
Below is the program to illustrate string::find_last_of():
CPP
// C++ program to illustrate string::find_last_of
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// Character to be found
char ch = 'e';
// To store the index of last
// character found
size_t found;
// Position till search is performed
int pos = 10;
// Function to find the last
// character ch in str[0, pos]
found = str.find_last_of(ch, pos);
// 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 last position
// of the character
else {
cout << "Character " << ch
<< " is found at index: "
<< found << endl;
}
}
Output: Character e is found at index: 6
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1).
References: https://cplusplus.com/reference/string/string/find_last_of/
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems