string::rbegin() and string::rend() in C++ (original) (raw)

Last Updated : 25 Sep, 2024

The std:🧵:rbegin() and **std:🧵:rend() functions in C++ are used to fetch the reverse iterators to the string. They are the member function of std::string and are used when we want to iterate the string in reverse. In this article, we will learn how to use string::rbegin() and string::rend() in C++.

string::rbegin()

The string::rbegin() function returns a reverse iterator pointing to the reverse beginning (i.e. last character) of the string.

If we increment the reverse iterator, it will move from right to left in the string i.e. it will move from last to second last and so on. It is just opposite to the normal string iterators.

Syntax

str.**rbegin()

where **str is the name of the string.

**Parameter

**Return value

string::rend()

The string::rend() is a reverse iterator pointing to the reverse end (i.e. theoretical character which is just before the first character) of the string.

Syntax

string_name.**rend()

**Parameter

**Return value

Example of string::rbegin() and string::rend()

C++ `

// C++ program to illustrate the use of // string::rbegin() and string::rend() function #include <bits/stdc++.h> using namespace std;

int main() { string s = "GfG htiW ecitcarP";

// Printing the character of string in
// reverse order
for (auto it = s.rbegin(); it != s.rend(); it++)
    cout << *it;

return 0;

}

`