|
| 1 | +--- |
| 2 | +Title: 'rfind()' |
| 3 | +Description: 'Searches a string from right to left and returns the last occurrence of a substring or character.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Game Development' |
| 7 | +Tags: |
| 8 | + - 'Containers' |
| 9 | + - 'Methods' |
| 10 | + - 'STL' |
| 11 | + - 'Strings' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The C++ **`rfind()`** [function](https://www.codecademy.com/resources/docs/cpp/functions) returns the position of the last occurrence of a given substring or character within a `std::string`. The search proceeds from right to left, and the function returns `std::string::npos` if no match is found. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +string_object.rfind(val, pos = npos); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `val`: A character or string to search for. |
| 28 | +- `pos` (Optional): The position to start searching backward from. Defaults to `std::string::npos`, meaning the end of the string. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns the index (zero-based) of the last occurrence of val, or `std::string::npos` if no match is found. |
| 33 | + |
| 34 | +## Example 1 |
| 35 | + |
| 36 | +In this example, `rfind()` locates the last occurrence of a substring: |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <iostream> |
| 40 | +#include <string> |
| 41 | + |
| 42 | +int main() { |
| 43 | + std::string text = "hello world, welcome to the world"; |
| 44 | + |
| 45 | + std::size_t pos = text.rfind("world"); |
| 46 | + std::cout << "Last occurrence of 'world': " << pos << "\n"; |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +The output of this code is: |
| 53 | + |
| 54 | +```shell |
| 55 | +Last occurrence of 'world': 28 |
| 56 | +``` |
| 57 | + |
| 58 | +## Example 2 |
| 59 | + |
| 60 | +In this example, `rfind()` is used to locate the last occurrence of a character: |
| 61 | + |
| 62 | +```cpp |
| 63 | +#include <iostream> |
| 64 | +#include <string> |
| 65 | + |
| 66 | +int main() { |
| 67 | + std::string text = "abracadabra"; |
| 68 | + |
| 69 | + std::size_t pos = text.rfind('a'); |
| 70 | + std::cout << "Last 'a' found at index: " << pos << "\n"; |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +The output of this code is: |
| 77 | + |
| 78 | +```shell |
| 79 | +Last 'a' found at index: 10 |
| 80 | +``` |
| 81 | + |
| 82 | +## Codebyte Example |
| 83 | + |
| 84 | +In this example, the position of the last occurrence of a character is printed using `rfind()`: |
| 85 | + |
| 86 | +```codebyte/cpp |
| 87 | +#include <iostream> |
| 88 | +#include <string> |
| 89 | +
|
| 90 | +int main() { |
| 91 | + std::string word = "mississippi"; |
| 92 | +
|
| 93 | + std::size_t pos = word.rfind('s'); |
| 94 | + std::cout << "Last 's' found at index: " << pos << "\n"; |
| 95 | +
|
| 96 | + return 0; |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Frequently Asked Questions |
| 101 | + |
| 102 | +### 1. What does `rfind` do in C++? |
| 103 | + |
| 104 | +The `rfind()` function searches a string from right to left and returns the index of the last occurrence of a character or substring. If the value is not found, it returns `std::string::npos`. It is useful when the needed match appears multiple times and the last one is required. |
| 105 | + |
| 106 | +### 2. What does `string::find()` do in C++? |
| 107 | + |
| 108 | +The `find()` function searches a string from left to right and returns the index of the first occurrence of a specified character or substring. Like `rfind()`, it returns `std::string::npos` when no match exists. |
| 109 | + |
| 110 | +### 3. What does `string()` do in C++? |
| 111 | + |
| 112 | +Calling `std::string()` constructs an empty string object. It represents a dynamic sequence of characters and supports operations such as insertion, erasure, concatenation, comparison, and searching. |
0 commit comments