Print reverse of a string using recursion
Write a recursive function to print the reverse of a given string.
Code:
C++
#include <bits/stdc++.h>
using namespace std;
void reverse(string str)
{
if (str.size() == 0)
{
return ;
}
reverse(str.substr(1));
cout << str[0];
}
int main()
{
string a = "Geeks for Geeks" ;
reverse(a);
return 0;
}
|
C
# include <stdio.h>
void reverse( char *str)
{
if (*str)
{
reverse(str+1);
printf ( "%c" , *str);
}
}
int main()
{
char a[] = "Geeks for Geeks" ;
reverse(a);
return 0;
}
|
Java
class StringReverse
{
void reverse(String str)
{
if ((str== null )||(str.length() <= 1 ))
System.out.println(str);
else
{
System.out.print(str.charAt(str.length()- 1 ));
reverse(str.substring( 0 ,str.length()- 1 ));
}
}
public static void main(String[] args)
{
String str = "Geeks for Geeks" ;
StringReverse obj = new StringReverse();
obj.reverse(str);
}
}
|
Python
def reverse(string):
if len (string) = = 0 :
return
temp = string[ 0 ]
reverse(string[ 1 :])
print (temp, end = '')
string = "Geeks for Geeks"
reverse(string)
|
C#
using System;
class GFG
{
static void reverse(String str)
{
if ((str == null ) || (str.Length <= 1))
Console.Write(str);
else
{
Console.Write(str[str.Length-1]);
reverse(str.Substring(0,(str.Length-1)));
}
}
public static void Main()
{
String str = "Geeks for Geeks" ;
reverse(str);
}
}
|
Javascript
<script>
function reverse(str, len) {
if (len == str.length) {
return ;
}
reverse(str, len + 1);
document.write(str[len]);
}
let a = "Geeks for Geeks" ;
reverse(a, 0);
</script>
|
PHP
<?php
function reverse( $str )
{
if (( $str == null) ||
( strlen ( $str ) <= 1))
echo ( $str );
else
{
echo ( $str [ strlen ( $str ) - 1]);
reverse( substr ( $str , 0,
( strlen ( $str ) - 1)));
}
}
$str = "Geeks for Geeks" ;
reverse( $str );
?>
|
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) where n is the length of string
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++
#include <bits/stdc++.h>
using namespace std;
void reverse( char *str, int index, int n)
{
if (index == n)
{
return ;
}
char temp = str[index];
reverse(str, index+1, n);
cout << temp;
}
int main()
{
char a[] = "Geeks for Geeks" ;
int n = sizeof (a) / sizeof (a[0]);
reverse(a, 0, n);
return 0;
}
|
C
#include <stdio.h>
void reverse( char *str, int index, int n)
{
if (index == n)
{
return ;
}
char temp = str[index];
reverse(str, index+1, n);
printf ( "%c" , temp);
}
int main() {
char a[] = "Geeks for Geeks" ;
int n = sizeof (a) / sizeof (a[0]);
reverse(a, 0, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void reverse( char [] str, int index, int n)
{
if (index == n)
{
return ;
}
char temp = str[index];
reverse(str, index + 1 , n);
System.out.print(temp);
}
public static void main(String[] args)
{
char a[] = "Geeks for Geeks" .toCharArray();
int n = a.length;
reverse(a, 0 , n);
}
}
|
Python3
def reverse(string, index, n):
if index = = n:
return
temp = string[index]
reverse(string, index + 1 , n)
print (temp, end = "")
string = "Geeks for Geeks"
n = len (string)
reverse(string, 0 , n)
|
C#
using System;
public class GFG
{
public static void reverse( char [] str, int index, int n)
{
if (index == n)
{
return ;
}
var temp = str[index];
GFG.reverse(str, index + 1, n);
Console.Write(temp);
}
public static void Main(String[] args)
{
char [] a = "Geeks for Geeks" .ToCharArray();
var n = a.Length;
GFG.reverse(a, 0, n);
}
}
|
Javascript
function reverse(string, index, n) {
if (index === n) {
return ;
}
var temp = string[index];
reverse(string, index+1, n);
console.log(temp);
}
var string = "Geeks for Geeks" ;
var n = string.length;
reverse(string, 0, n);
|
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.
C++
#include <iostream>
using namespace std;
string reverse(string str, int len) {
if (len < 1) {
return "" ;
}
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 Main {
public static String reverse(String str, int len) {
if (len < 1 ) {
return "" ;
}
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()));
}
}
|
Python3
def reverse(string, length):
if length < 1 :
return ""
if length = = 1 :
return string[ 0 ]
return string[length - 1 ] + reverse(string, length - 1 )
if __name__ = = "__main__" :
input_str = "Geeks for geeks"
print (reverse(input_str, len (input_str)))
|
C#
using System;
class Program
{
static string Reverse( string str, int len)
{
if (len < 1)
{
return "" ;
}
if (len == 1)
{
return str[0].ToString();
}
return str[len - 1] + Reverse(str, len - 1);
}
static void Main()
{
string str = "Geeks for geeks" ;
Console.WriteLine(Reverse(str, str.Length));
}
}
|
Javascript
function reverse(str , len){
if (len < 1){
return
}
if (len === 1){
return str[0]
}
return str[len-1] + reverse(str , len -1 )
}
let str = "Geeks for geeks"
console.log(reverse(str, str.length))
|
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.
Explanation: This problem can be expressed 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”
Please suggest if someone has a better solution which is more efficient in terms of space and time.
This article is contributed by Anurag Mishra and Aarti_Rathi .
Last Updated :
16 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...