Print reverse of a string using recursion

Write a recursive function to print reverse of a given string.

Program:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to reverse a string using recursion
# include <stdio.h>
  
/* Function to print reverse of the passed string */
void reverse(char *str)
{
   if (*str)
   {
       reverse(str+1);
       printf("%c", *str);
   }
}
  
/* Driver program to test above function */
int main()
{
   char a[] = "Geeks for Geeks";
   reverse(a);
   return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reverse a string using recursion
  
class StringReverse
{
    /* Function to print reverse of the passed string */
    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));
        }
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        String str = "Geeks for Geeks";
        StringReverse obj = new StringReverse();
        obj.reverse(str);
    }    
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to reverse a string using recursion
  
# Function to print reverse of the passed string
def reverse(string):
    if len(string) == 0:
        return
      
    temp = string[0]
    reverse(string[1:])
    print(temp, end='')
  
# Driver program to test above function
string = "Geeks for Geeks"
reverse(string)
  
# A single line statement to reverse string in python
# string[::-1]
  
# This code is contributed by Bhavya Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reverse 
// a string using recursion
using System;
  
class GFG
{
    // Function to print reverse
    // of the passed string
    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)));
        }
    }
      
    // Driver Code
    public static void Main() 
    {
        String str = "Geeks for Geeks";
        reverse(str);
    
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to reverse 
// a string using recursion
  
// Function to print reverse
// of the passed string
function reverse($str)
{
    if (($str == null) || 
        (strlen($str) <= 1))
    echo ($str);
  
    else
    {
        echo ($str[strlen($str) - 1]);
        reverse(substr($str, 0, 
               (strlen($str) - 1)));
    }
}
  
// Driver Code
$str = "Geeks for Geeks";
reverse($str);
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

chevron_right



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 pointer reaches ‘\0’, all functions accumulated in stack print char at passed location (str) and return one by one.



Time Complexity: O(n)

See Reverse a string for other methods to reverse string.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.