Print reverse of a string using recursion Last Updated : 06 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Given a string, the task is to print the given string in reverse order using recursion.Examples:Input: s = "Geeks for Geeks"Output: "skeeG rof skeeG"Explanation: After reversing the input string we get "skeeG rof skeeG". Input: s = "Reverse a string Using Recursion"Output: "noisruceR gnisU gnirts a esreveR"Explanation: After reversing the input string we get "noisruceR gnisU gnirts a esreveR". [Approach - 1] - Make a Recursive Call and Then Process the First CharThe idea for this approach is to make a recursive call for the substring starting from the second character and then print the first character. C++ #include <bits/stdc++.h> using namespace std; string reverse(string str) { if(str.size() == 0) return str; return reverse(str.substr(1)) + str[0]; } int main() { string str = "Geeks for Geeks"; cout << reverse(str); return 0; } C #include <stdio.h> void reverse(const char *str) { if (*str == '\0') return; reverse(str + 1); putchar(*str); } int main() { char str[] = "Geeks for Geeks"; reverse(str); return 0; } Java public class Main { public static String reverse(String str) { if (str.isEmpty()) return str; return reverse(str.substring(1)) + str.charAt(0); } public static void main(String[] args) { String str = "Geeks for Geeks"; System.out.println(reverse(str)); } } Python # Function to reverse a string def reverse(str): if len(str) == 0: return str return reverse(str[1:]) + str[0] # Main function if __name__ == '__main__': str = 'Geeks for Geeks' print(reverse(str)) C# using System; class Program { static string Reverse(string str) { if (str.Length == 0) return str; return Reverse(str.Substring(1)) + str[0]; } static void Main() { string str = "Geeks for Geeks"; Console.WriteLine(Reverse(str)); } } JavaScript // Function to reverse a string function reverse(str) { if (str.length === 0) return str; return reverse(str.substring(1)) + str[0]; } // Main function const str = 'Geeks for Geeks'; console.log(reverse(str)); OutputskeeG rof skeeGTime Complexity: O(n)Auxiliary Space: O(n)[Approach - 2] - Process the Last Char and Then Make Recursive CallThis idea is to break the problem in terms of smaller instance of same subproblem. str= "Geeks for geeks"reverse string of str = last character of str + reverse string of remaining str = "s" + reverse string of "Geeks for geek" = "skeeg rof skeeG" C++ #include <bits/stdc++.h> using namespace std; string reverse(string str, int len) { if (len < 1) { return ""; } // Base case if (len == 1) { return string(1, str[0]); } return str[len - 1] + reverse(str, len - 1); } int main() { string str = "Geeks for geeks"; cout << reverse(str, str.length()) << endl; return 0; } Java import java.util.*; public class GfG { // Function to reverse a string using recursion public static String reverse(String str, int len) { if (len < 1) { return ""; } // Base case if (len == 1) { return String.valueOf(str.charAt(0)); } return str.charAt(len - 1) + reverse(str, len - 1); } public static void main(String[] args) { String str = "Geeks for geeks"; System.out.println(reverse(str, str.length())); } } Python def reverse(string, length): if length < 1: return "" # Base case if length == 1: return string[0] return string[length - 1] + reverse(string, length - 1) if __name__ == "__main__": str = "Geeks for geeks" print(reverse(str, len(str))) C# using System; class GfG { static string Reverse(string str, int len) { // Base case: If the string length is less than 1, // return an empty string if (len < 1) { return ""; } // Base case: If the string has only one character, // return that character as a string if (len == 1) { return str[0].ToString(); } // Recursive case: Concatenate the last character of // the string with the reversed substring return str[len - 1] + Reverse(str, len - 1); } static void Main() { string str = "Geeks for geeks"; // Call the Reverse function and print the reversed // string Console.WriteLine(Reverse(str, str.Length)); } } JavaScript function reverse(str, len) { if (len < 1) { return } // base case if (len === 1) { return str[0] } return str[len - 1] + reverse(str, len - 1) } // Driver code let str = "Geeks for geeks" console.log(reverse(str, str.length)) Outputskeeg rof skeeGTime Complexity: O(n)Auxiliary Space: O(n)[Approach - 3] - Optimized- Process from Both EndsThe idea is to begin with both corners, swap the corner characters and then make recursive call for the remaining string. C++ #include <bits/stdc++.h> using namespace std; void reverse(string &str, int start, int end) { if (start >= end) return; swap(str[start], str[end]); reverse(str, start + 1, end - 1); } int main() { string str = "Geeks for Geeks"; reverse(str, 0, str.size() - 1); cout << str; return 0; } Java // Java program to reverse a string class ReverseString { static void reverse(StringBuilder str, int start, int end) { if (start >= end) return; char temp = str.charAt(start); str.setCharAt(start, str.charAt(end)); str.setCharAt(end, temp); reverse(str, start + 1, end - 1); } public static void main(String[] args) { StringBuilder str = new StringBuilder("Geeks for Geeks"); reverse(str, 0, str.length() - 1); System.out.println(str); } } Python # Python program to reverse a string def reverse(str, start, end): if start >= end: return str[start], str[end] = str[end], str[start] # Swap characters reverse(str, start + 1, end - 1) if __name__ == '__main__': str = list("Geeks for Geeks") reverse(str, 0, len(str) - 1) print(''.join(str)) JavaScript // JavaScript implementation function reverse(str, start, end) { if (start >= end) return; str = str.split(''); [str[start], str[end]] = [str[end], str[start]]; str = str.join(''); reverse(str, start + 1, end - 1); return str; } let str = "Geeks for Geeks"; str = reverse(str, 0, str.length - 1); console.log(str); Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Print reverse of a string using recursion A Anurag Mishra and Aarti_Rathi Improve Article Tags : Strings Recursion DSA Practice Tags : RecursionStrings Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like