C++ Program To Print Reverse of a String Using Recursion Last Updated : 17 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Write a recursive function to print the reverse of a given string. Code: C++ // C++ program to reverse a string using recursion #include <bits/stdc++.h> using namespace std; /* Function to print reverse of the passed string */ void reverse(string str) { if(str.size() == 0) { return; } reverse(str.substr(1)); cout << str[0]; } /* Driver program to test above function */ int main() { string a = "Geeks for Geeks"; reverse(a); return 0; } // This is code is contributed by rathbhupendra Output: skeeG rof skeeG Explanation: Recursive function (reverse) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way when the pointer reaches '\0', all functions accumulated in stack print char at passed location (str) and return one by one. Time Complexity: O(n^2) as substr() method has a time complexity of O(k) where k is the size of the returned string. So for every recursive call, we are reducing the size of the string by one, which leads to a series like (k-1)+(k-2)+...+1 = k*(k-1)/2 = O(k^2) = O(n^2)See Reverse a string for other methods to reverse string.Auxiliary Space: O(n) Efficient Approach: We can store each character in recursive stack and then can print while coming back as shown in the below code: C++ // C++ program to reverse a string using recursion #include <bits/stdc++.h> using namespace std; /* Function to print reverse of the passed string */ void reverse(char *str, int index, int n) { if(index == n) // return if we reached at last index or at the end of the string { return; } char temp = str[index]; // storing each character starting from index 0 in function call stack; reverse(str, index+1, n); // calling recursive function by increasing index everytime cout << temp; // printing each stored character while recurring back } /* Driver program to test above function */ int main() { char a[] = "Geeks for Geeks"; int n = sizeof(a) / sizeof(a[0]); reverse(a, 0, n); return 0; } Output: skeeG rof skeeG Time Complexity: O(n) where n is size of the string Auxiliary Space: O(n) where n is the size of string, which will be used in the form of function call stack of recursion. Please suggest if someone has a better solution that is more efficient in terms of space and time. Comment More infoAdvertise with us Next Article C++ Program To Print Reverse of a String Using Recursion kartik Follow Improve Article Tags : C++ Programs C++ CPP Strings Programs Practice Tags : CPP Similar Reads C++ Program to Reverse a String Using Stack Given a string, reverse it using stack. For example "GeeksQuiz" should be converted to "ziuQskeeG". Following is simple algorithm to reverse a string using stack. 1) Create an empty stack.2) One by one push all characters of string to stack.3) One by one pop all characters from stack and put them ba 4 min read C++ Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p 7 min read C++ Program To Print Reverse Floyd's Triangle Floydâs triangle is a triangle with first natural numbers. Task is to print reverse of Floydâs triangle.Examples: Input: 4 Output: 10 9 8 7 6 5 4 3 2 1 Input: 5 Output: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 C++ // C++ program to print reverse // of Floyd's triangle #include <bits/stdc++.h> using 1 min read Program to reverse words in a given string in C++ Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++. Examples: Input: str = "the sky is blue" Output: blue is sky theInput: str = "I love programming" Output: programming love I Method 1: Using STL functions Reverse the given string str using STL 6 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse a Word Using Stack in C++? In C++, we have a stack data structure that follows the Last In First Out (LIFO) principle. In this article, we will learn how to reverse a word using a stack in C++. Example: Input:word = "GeeksforGeeks"Output:Reversed Word: skeeGrofskeeGReverse a String Using Stack in C++To reverse a word using a 2 min read C++ Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit 2 min read Reverse substrings between each pair of parenthesis Given a string str that consists of lowercase English letters and brackets. The task is to reverse the substrings in each pair of matching parentheses, starting from the innermost one. The result should not contain any brackets. Examples: Input: str = "(skeeg(for)skeeg)" Output: geeksforgeeks Input: 9 min read Like