C++ String Rend Function - C++ Programming Tutorial
C++ Course / Strings / C++ String Rend Function

C++ String Rend Function

BLUF: Mastering C++ String Rend Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ String Rend Function

C++ is renowned for its efficiency. Learn how C++ String Rend Function enables low-level control and high-performance computing in the tutorial below.

In C++, the rend method in the string class within the STL signifies the reverse end. This function is primarily employed to provide a reverse iterator pointing to the element before the first element in the string. It is frequently used to iterate through the string in reverse, processing it from the end to the beginning.

Syntax:

It has the following syntax:

Example

reverse_iterator rend();

const_reverse_iterator rend() const;

In this syntax,

Parameter: This function does not include any arguments.

It provides a reverse iterator that points to the start of the string.

Simple C++ String rend Function Example

Let's consider a basic example to demonstrate the functionality of the rend method in C++.

Example

Example

#include<iostream>  

using namespace std;   //using standard namespace

int main()    //main function

{  

    string str="Hello World";  

    for(string::reverse_iterator itr=str.rbegin();itr!=str.rend();++itr)  

    cout<<*itr;  

    return 0;  

}

Output:

Output

dlroW olleH

Explanation:

In this instance, we have employed the rbegin method in conjunction with the rend function to traverse through the "Hello World" string in a reverse manner. Subsequently, rbegin points to the last character of the string, whereas rend signifies the conclusion of the reverse iteration. As a result, the string is exhibited in reverse sequence as "dlroW olleH".

C++ String rend function Example with Constant String

Let's consider an example to demonstrate the rend function for constant strings in C++.

Example

Example

#include <iostream>

#include <string>

using namespace std;  //using standard namespace

int main() {  //main function

    const string str = "C++ is a programming language";

    cout <<" The reversed order of the string is: ";

    for (auto it = str.crbegin(); it != str.crend(); ++it) {

        cout << *it;

    }

    cout << endl;

    return 0;

}

Output:

Output

The reversed order of the string is: egaugnal gnimmargorp a si ++C

Explanation:

In this instance, a constant string named str has been initialized with the content " C++ is a programming language ". Subsequently, the crbegin and crend methods are employed to traverse the string in a reverse manner. These methods offer constant reverse iterators, allowing for reading of elements without modification. While iterating from the end towards the beginning, the loop exhibits the string in reverse sequence on the console.

C++ Example to check if a string is a palindrome using the rend function

Let's consider a scenario to demonstrate the verification of a string as a palindrome using the rend function in C++.

Example

Example

#include <iostream>

#include <string>

using namespace std;  //using standard namespace

  

int main() {  //main function

    string str = "racecar";

    string rev;

    for (auto it = str.rbegin(); it != str.rend(); ++it)

        rev += *it;

    if (str == rev)

        cout << "The given string is a palindrome." << endl;

    else

        cout << "The given string is not a palindrome." << endl;

    return 0;

}

Output:

Output

The given string is a palindrome.

Explanation:

In this instance, a variable named str is established with the initial value of "racecar". Subsequently, the rbegin and rend functions are employed to traverse the string in reverse and save the reversed sequence in the variable rev. Following this, a comparison is made between the original string and its reversed form to ascertain if they are identical. Since the word "racecar" remains unchanged when read forwards and backwards, a notification is displayed indicating that the provided string is a palindrome.

C++ Example to count vowels in reverse order using the rend function

Let's consider an example to showcase how to count vowels in reverse sequence by utilizing the string rend method in C++.

Example

Example

#include <iostream>

#include <string>

using namespace std;    //using standard namespace

int main() {    //main function

    string str = "Adventitious";

    int vowels = 0;

    for (auto it = str.rbegin(); it != str.rend(); ++it) {

        if (*it == 'a' || *it == 'e' || *it == 'i' || *it == 'o' || *it == 'u' ||

            *it == 'A' || *it == 'E' || *it == 'I' || *it == 'O' || *it == 'U')

            vowels++;

    }

    cout << "Number of vowels (checked in reverse): " << vowels << endl;

    return 0;

}

Output:

Output

Number of vowels (checked in reverse): 6

Explanation:

In this instance, a string named str has been established with the content "Adventitious". Subsequently, the reverse iterator methods rbegin and rend have been employed to traverse the string in a backward manner. Upon completion of the traversal process, each character is inspected to determine if it qualifies as a vowel, leading to an increment in the vowel count. Ultimately, the program displays the overall count of vowels identified within the string.

Key points of the rend Function in C++

The string rend function has many key points in C++. Some of them are as follows:

  • It is commonly utilized with the rbegin function to traverse a string in the reverse order.
  • The rend function is used to return a reverse iterator thacpp tutorials to the theoretical element before the first character of the string.
  • When we use the rend function with a non-const string, the string element cannot be changed via the reverse iterator. If we utilize this function with const strings, the elements are read-only in C++.
  • In C++, the iterator returned by the rend function doesn't refer to a valid character. It marks the end boundary for the reverse iteration.
  • It takes only constant time complexity O(1) to perform several operations.
  • Conclusion:

In summary, the string::rend method in C++ provides a straightforward and efficient approach for traversing strings in reverse. This function is frequently paired with rbegin to conveniently retrieve characters from the end to the beginning without the need for manual indexing. It is commonly employed in tasks like reverse iteration, palindrome detection, string examination, and character manipulation starting from the end. In general, leveraging rend and rbegin can significantly enhance the clarity, safety, and efficiency of string-related code.

C++ String rend Function FAQs

1) What is the string rend function in C++?

The rend method is an inherent member function within the string class in the STL. It is frequently employed to retrieve a reverse iterator pointing to the element before the first element.

The primary contrast between the rbegin and rend functions in C++ lies in their directionality. The rbegin function is used to obtain a reverse iterator pointing to the last element in a container, while the rend function retrieves a reverse iterator pointing to the theoretical element preceding the first element in the container.

The primary contrast between the rbegin method and the rend method lies in their intended use cases. While rbegin is frequently employed to reference the final element within the string, rend is typically used to reference a position before the first element in C++.

The return type of the rend function in C++ is an iterator pointing to the element preceding the first element in a reverse order container.

The rend function's return type is the reverse_iterator, which enables backward traversal of the string.

4) Does the rend function modify the original string in C++?

No, the rend function in C++ solely provides an iterator for reverse traversal. It does not alter the string's data.

5) What is the time complexity of the string rend method in C++?

If we want to perform any operation using the rend function, it takes only constant time O(1) because it just returns a pre-defined iterator that shows the reverse end of the container in C++.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience