std::string::crbegin() and std::string::crend() in C++ with Examples Last Updated : 10 Jul, 2022 Comments Improve Suggest changes Like Article Like Report std::string::crbegin() The std::string::crbegin() is a string class built-in function that returns a constant reverse iterator referring to the last element in the string. Using this iterator starts the string traversal from the end of the string. Header File: #include <string> Template Class: template <class C> auto crbegin( const C& c ) -> decltype(std::rbegin(c)); Syntax: string_name.crbegin() Parameters: This function doesn't require any parameters. Return Value: This function std::string::crbegin() returns a constant reverse iterator referring to the last element in the string. Below is the program to illustrate string::crbegin(): Program 1: CPP // C++ program to illustrate // std::string:crbegin() #include <iostream> #include <string> using namespace std; // Driver Code int main() { // Given string string str("GeeksForGeeks"); // Traverse the given string using // reverse iterator crbegin() for (auto it = str.crbegin(); it != str.crend(); it++) { // Print the elements cout << *it; } return 0; } Output:skeeGroFskeeG Time Complexity: O(N) // Where N is the length of the string.Auxiliary Space: O(1) std::string::crend() The std::string::crend() is a string class built-in function that returns a constant reverse iterator pointing to the theoretical element before the first element in the string. This iterator is used to reach the starting of the string while traversing the string in reverse order. Template Class: template <class C> auto crend( const C& c ) -> decltype(std::rend(c)); Syntax: string_name.crend() Parameters: This function doesn't require any parameters. Return Value: This function std::string::crend() returns a constant reverse iterator pointing to the element before the first element in the string. Below is the program to illustrate string::crend(): Program 2: CPP // C++ program to illustrate // std::string:crend() #include <iostream> #include <string> using namespace std; // Driver Code int main() { // Given string string str("GeeksForGeeks"); // Find string length int N = str.length(); // Given character char ch = 'k'; // To check whether the char is // present or not bool a = true; // Traverse the given string using // reverse iterator crbegin() and // check if ch is present or not for (auto it = str.crbegin(); it != str.crend(); it++) { if (*it == ch) { cout << "The last index is " << N - (it - str.crbegin() + 1) << endl; a = false; break; } } if (a) { cout << "Character is not present"; } return 0; } Output:The last index is 11 Time Complexity: O(N) // Where N is the length of the string.Auxiliary Space: O(1) Let us see the differences in a tabular form -: std::string::crbegin()std::string::crend() 1.It is used to return a const_reverse_iterator pointing to the last character of the stringIt is used to return a const_reverse_iterator pointing to the theoretical character preceding the first character of the string2. Its syntax is -: const_reverse_iterator crbegin() Its syntax is -: const_reverse_iterator crend(); 3.It does not take any parameters.It does not take any parameters.4.Its complexity is constant.Its complexity is constant.5.Its iterator validity does not changes.Its iterator validity does not changes. Comment More infoAdvertise with us Next Article std::string::crbegin() and std::string::crend() in C++ with Examples P pranjal_g99 Follow Improve Article Tags : Strings C Programs C++ Programs C Language C++ DSA STL cpp-string CPP-Functions C-String C-Functions C-Library cpp-strings +9 More Practice Tags : CPPcpp-stringsSTLStrings Similar Reads std::string::rfind in C++ with Examples The std::string::rfind is a string class member function that is used to search the last occurrence of any character in the 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 will return string::npos which denote 5 min read Difference Between std::wstring and std::string The std::wstring and std::string are the classes in C++ used to store sequences of characters. While serving similar purposes, they serve different requirements. In this article, we will look at some major differences between the std::wstring and std::string in C++. Wide String in C++The std::wstrin 3 min read starts_with() and ends_with() in C++20 with Examples In this article, we will be discussing starts_with() and ends_with() with examples in C++20. starts_with() This function efficiently checks if a string begins with the given prefix or not. This function written in both std::basic_string and in std::basic_string_view. Syntax: template <typename P 3 min read Difference Between Null Strings and Empty String in C++ In C++, we generally initialize a string that does not store anything at declaration as either a null string or an empty string. They both may seem similar to each other but they are different in terms of what they store or refer to. In this article, we will discuss the key differences between the n 3 min read How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor 1 min read How to Handle Multiple String Inputs with Spaces in C++? In C++, strings are a sequence of characters that might contain spaces in many cases but we can read only the input text till whitespace using cin. In this article, we will learn how to handle multiple string inputs with spaces in C++. Example: Input:Enter multiple strings:String1String2Output:You E 2 min read Processing strings using std::istringstream The std::istringstream is a string class object which is used to stream the string into different variables and similarly files can be stream into strings. Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed as a string object 5 min read How to Create a Stack of Strings in C++? In C++, Stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char* 3 min read How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co 4 min read Like