Skip to content
Related Articles

Related Articles

Program to reverse a string (Iterative and Recursive)
  • Difficulty Level : Easy
  • Last Updated : 31 Mar, 2021

Given a string, write a recursive program to reverse it.
 

string-reverse

 

Method 1 (Using Stack) 
 

C++




// C++ program to reverse a string using stack
#include <bits/stdc++.h>
using namespace std;
 
void recursiveReverse(string &str)
{
   stack<char> st;
   for (int i=0; i<str.length(); i++)
       st.push(str[i]);
 
   for (int i=0; i<str.length(); i++) {
       str[i] = st.top();
       st.pop();
   }      
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    recursiveReverse(str);
    cout << str;
    return 0;
}

Java




// Java program to reverse a string using stack
import java.util.*;
class GFG
{
  public static String recursiveReverse(char []str)
 {
   Stack<Character> st = new Stack<>();
   for(int i=0; i<str.length; i++)
        st.push(str[i]);
 
   for (int i=0; i<str.length; i++) {
    str[i] = st.peek();
    st.pop();
   }    
   return String.valueOf(str);// converting character array to string
 }
 
// Driver program
   public static void main(String []args)
   {
      String str = "geeksforgeeks";
      str = recursiveReverse(str.toCharArray());// passing character array as parameter
      System.out.println(str);
   }
}
// This code is contributed by Adarsh_Verma

Python3




# Python program to reverse a string using stack
 
def recursiveReverse(str):
     
    # using as stack
    stack = []
 
    for i in range(len(str)):
        stack.append(str[i])
     
    for i in range(len(str)):
        str[i] = stack.pop()
 
if __name__ == "__main__":
    str = "geeksforgeeks"
 
    # converting string to list
    # because strings do not support
    # item assignment
    str = list(str)
    recursiveReverse(str)
 
    # converting list to string
    str = ''.join(str)
    print(str)
     
# This code is contributed by
# sanjeev2552

C#




// C# program to reverse a string using stack
using System;
using System.Collections.Generic;
 
class GFG
{
    public static String recursiveReverse(char []str)
    {
        Stack<char> st = new Stack<char>();
        for(int i = 0; i < str.Length; i++)
            st.Push(str[i]);
 
        for (int i = 0; i < str.Length; i++)
        {
            str[i] = st.Peek();
            st.Pop();
        }
         
        // converting character array to string
        return String.Join("",str);
    }
 
    // Driver program
    public static void Main()
    {
        String str = "geeksforgeeks";
         
        // passing character array as parameter
        str = recursiveReverse(str.ToCharArray());
        Console.WriteLine(str);
    }
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
      // JavaScript program to reverse a string
 
      function recursiveReverse(str)
      {
        var revString = "";
 
        for (var i = str.length - 1; i >= 0; i--)
        {
          revString += str[i];
        }
 
        return revString;
      }
 
      // Driver program
      var str = "geeksforgeeks";
      document.write(recursiveReverse(str));
       
      // This code is contributed by rdtank.
    </script>
Output: 



skeegrofskeeg

 

Time complexity : O(n) 
Auxiliary Space : O(n)
Method 2 (Iterative)
 

C++




// A Simple Iterative C++ program to reverse
// a string
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse a string
void reverseStr(string& str)
{
    int n = str.length();
 
    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}

Java




// A Simple Java program
// to reverse a string
import java.util.Scanner;
 
public class reverseStr
{
// Function to reverse
// a string
void stringReverse()
{
    String str = "geeksforgeeks";
    int length = str.length();
    StringBuffer revString = new StringBuffer();
    for (int i = length - 1;
             i >= 0; i--)
    {
        revString.append(str.charAt(i));
    }
    System.out.println(revString);
}
 
// Driver Code
public static void main(String []args)
{
    reverseStr s= new reverseStr();
    s.stringReverse();
}
}
 
// This code is contributed
// by prabhat kumar singh

Python




# A Simple python program
# to reverse a string
 
# Function to
# reverse a string
def reverseStr(str):
    n = len(str)
     
    # initialising a empty
    # string 'str1'
    str1 = ''
    i = n - 1
    while i >= 0:
         
        # copy str
        # to str1
        str1 += str[i]
        i -= 1
    print(str1)    
 
# Driver Code
def main():
    str = "geeksforgeeks";
    reverseStr(str);
     
if __name__=="__main__":
    main()    
     
# This code is contributed
# by prabhat kumar singh

C#




// A Simple Iterative C# program to reverse
// a string
using System;
 
class GFG
{
 
// Function to reverse a string
static String reverseStr(String str)
{
    int n = str.Length;
 
    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        str = swap(str,i,n - i - 1);
    return str;
}
static String swap(String str, int i, int j)
{
    char []ch = str.ToCharArray();
    char temp = ch[i];
    ch[i] = ch[j];
    ch[j] = temp;
    return String.Join("",ch);
}
 
// Driver code
public static void Main(String[] args)
{
    string str = "geeksforgeeks";
    str= reverseStr(str);
    Console.WriteLine(str);
}
}
 
// This code is contributed by Princi Singh

PHP




<?php
// A Simple Iterative PHP
// program to reverse
// a string
 
// Function to reverse a string
function reverseStr(&$str)
{
    $n = strlen($str);
 
    // Swap character starting
    // from two corners
    for ($i = 0; $i < $n / 2; $i++)
        //swap the string
        list($str[$i],
             $str[$n - $i - 1]) = array($str[$n - $i - 1],
                                        $str[$i]);
}
 
// Driver Code
$str = "geeksforgeeks";
 
reverseStr($str);
echo $str;
 
// This code is contributed by ajit
?>
Output: 
skeegrofskeeg

 

Time complexity : O(n) 
Auxiliary Space : O(1)
Method 3 (Iterative using two pointers) 
 

C++




// A Simple Iterative C++ program to reverse
// a string
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse a string
void reverseStr(string& str)
{
    int n = str.length();
 
    // Swap character starting from two
    // corners
    for (int i=0, j=n-1; i<j; i++,j--)
        swap(str[i], str[j]); 
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}

Java




//A Simple Iterative Java program to reverse
//a string
class GFG {
 
    //Function to reverse a string
    static void reverseStr(String str)
    {
     int n = str.length();
     char []ch = str.toCharArray();
     char temp;
 
     // Swap character starting from two
     // corners
     for (int i=0, j=n-1; i<j; i++,j--)
     {
         temp = ch[i];
         ch[i] = ch[j];
         ch[j] = temp;
     }
         
      
     System.out.println(ch);
    }
 
    //Driver program
    public static void main(String[] args) {
         
        String str = "geeksforgeeks";
         reverseStr(str);
    }
}
// This code is contributed by Ita_c.

Python3




# A Simple Iterative Python program to
# reverse a string
 
# Function to reverse a string
def reverseStr(str):
    n = len(str)
 
    i, j = 0, n-1
 
    # Swap character starting from
    # two corners
    while i < j:
        str[i], str[j] = str[j], str[i]
 
        i += 1
        j -= 1
 
 
# Driver code
if __name__ == "__main__":
    str = "geeksforgeeks"
 
    # converting string to list
    # because strings do not support
    # item assignment
    str = list(str)
    reverseStr(str)
 
    # converting list to string
    str = ''.join(str)
 
    print(str)
 
# This code is contributed by
# sanjeev2552

C#




// A Simple Iterative C# program 
// to reverse a string
using System;
 
class GFG
{
 
    //Function to reverse a string
    static void reverseStr(String str)
    {
        int n = str.Length;
        char []ch = str.ToCharArray();
        char temp;
 
        // Swap character starting from two
        // corners
        for (int i=0, j=n-1; i<j; i++,j--)
        {
            temp = ch[i];
            ch[i] = ch[j];
            ch[j] = temp;
        }
        Console.WriteLine(ch);
    }
 
    //Driver program
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
        reverseStr(str);
    }
}
 
// This code is contributed by PrinciRaj1992

PHP




<?php
// A Simple Iterative PHP
// program to reverse a string
 
// Function to reverse a string
function reverseStr (&$str)
{
    $n = strlen($str);
 
    // Swap character starting
    // from two corners
    for ($i = 0, $j = $n - 1;
         $i < $j; $i++, $j--)
        //swap function
        list($str[$i],
             $str[$j]) = array($str[$j],
                               $str[$i]);
}
 
// Driver Code
$str = "geeksforgeeks";
reverseStr($str);
echo $str;
 
// This code is contributed by ajit.
?>
Output: 
skeegrofskeeg

 

Time complexity : O(n) 
Auxiliary Space : O(1)
Method 4 (Recursive) 
 

C++




// Recursive C++ program to reverse a string
#include <bits/stdc++.h>
using namespace std;
 
void recursiveReverse(string &str, int i = 0)
{
    int n = str.length();
    if (i == n / 2)
        return;
    swap(str[i], str[n - i - 1]);
    recursiveReverse(str, i + 1);
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    recursiveReverse(str);
    cout << str;
    return 0;
}

Java




// Recursive Java program to reverse a string
class GFG
{
 
static void recursiveReverse(char[] str, int i)
{
    int n = str.length;
    if (i == n / 2)
        return;
    swap(str,i,n - i - 1);
    recursiveReverse(str, i + 1);
}
static char[] swap(char []arr, int i, int j)
{
    char temp= arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    return arr;
}
 
// Driver program
public static void main(String[] args)
{
    char[] str = "geeksforgeeks".toCharArray();
    recursiveReverse(str,0);
    System.out.println(String.valueOf(str));
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Recursive Python program to reverse a string
 
def recursiveReverse(str, i = 0):
    n = len(str)
 
    if i == n // 2:
        return
     
    str[i], str[n-i-1] = str[n-i-1], str[i]
 
    recursiveReverse(str, i+1)
 
if __name__ == "__main__":
    str = "geeksforgeeks"
 
    # converting string to list
    # because strings do not support
    # item assignment
    str = list(str)
    recursiveReverse(str)
 
    # converting list to string
    str = ''.join(str)
    print(str)
 
# This code is contributed by
# sanjeev2552

C#




// Recursive C# program to reverse a string
using System;
 
public class GFG
{
  
static void recursiveReverse(char[] str, int i)
{
    int n = str.Length;
    if (i == n / 2)
        return;
    swap(str,i,n - i - 1);
    recursiveReverse(str, i + 1);
}
static char[] swap(char []arr, int i, int j)
{
    char temp= arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    return arr;
}
  
// Driver program
public static void Main(String[] args)
{
    char[] str = "geeksforgeeks".ToCharArray();
    recursiveReverse(str,0);
    Console.WriteLine(String.Join("",str));
}
}
// This code is contributed by Princi Singh

C++




// A quickly written program for reversing a string
// using reverse()
#include<bits/stdc++.h>
using namespace std;
int main()
{
   string str = "geeksforgeeks";
     
   // Reverse str[beign..end]
   reverse(str.begin(),str.end());
     
   cout << str;
   return 0;
}

Java




// A Simple Java program
// to reverse a string
 
class GFG
{
 
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
         
        // Reverse str[beign..end]
        str = reverse(str);
 
        System.out.println(str);
    }
 
    static String reverse(String input)
    {
        char[] temparray = input.toCharArray();
        int left, right = 0;
        right = temparray.length - 1;
 
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.valueOf(temparray);
    }
}
 
// This code is contributed by 29AjayKumar

Python




# A Simple python program
# to reverse a string
 
# Function to
# reverse a string
def reverseStr(str):
     
    # print the string
    # from last
    print(str[::-1])    
 
# Driver Code
def main():
    str = "geeksforgeeks";
    reverseStr(str);
     
if __name__=="__main__":
    main()    
     
# This code is contributed
# by prabhat kumar singh

C#




// A Simple C# program to reverse a string
using System;
     
class GFG
{
 
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
         
        // Reverse str[beign..end]
        str = reverse(str);
 
        Console.WriteLine(str);
    }
 
    static String reverse(String input)
    {
        char[] temparray = input.ToCharArray();
        int left, right = 0;
        right = temparray.Length - 1;
 
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.Join("",temparray);
    }
}
 
/* This code is contributed by PrinciRaj1992 */

PHP




<?php
// A Simple PHP program
// to reverse a string
 
// Function to reverse a string
function reverseStr($str)
{
 
    // print the string
    // from last
    echo strrev($str);
}
 
// Driver Code
$str = "geeksforgeeks";
 
reverseStr($str);
 
// This code is contributed
// by Srathore
?>
Output: 
skeegrofskeeg

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :