
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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 <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