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():
// 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 las // 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; } } |
Character e is found at index: 21
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():
// 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; } } |
Character e is found at index: 6
References: http://www.cplusplus.com/reference/string/string/find_last_of/
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.
Recommended Posts:
- isalpha() and isdigit() functions in C with cstring examples.
- Pointers in C/C++ with Examples
- strtok() and strtok_r() functions in C with examples
- memset() in C with examples
- std::mismatch() with examples in C++
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- set_symmetric_difference in C++ with Examples
- ratio_equal() in C++ with examples
- std::equal_to in C++ with Examples
- quick_exit() function in C++ with Examples
- feclearexcept in C++ with Examples
- Understanding Lvalues, PRvalues and Xvalues in C/C++ with Examples
- multiset lower_bound() in C++ STL with Examples
- multiset upper_bound() in C++ STL with Examples
- multiset max_size() in C++ STL with Examples
- forward_list max_size() in C++ STL with Examples
- std::allocator() in C++ with Examples
- array data() in C++ STL with Examples
- multiset size() in C++ STL with Examples
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.

