Home » Technical Interview Questions » String Interview Questions » Print Reverse of a string (Recursion)

Print Reverse of a string (Recursion)

Reading Time - 1 mins

Print the reverse of a string using recursion.


a) Input string : Sachin
Output : nihcas

b) Input string : Code
Output : edoC

Time complexity : O(n)


Create Reverse function to reverse string recursively
In this function,

a. It takes string pointer (str) as input.

b. It calls itself (recursively) with next location to passes pointer (str+1).

c. Continue recursion till it reaches the end of the string.

d. After all functions accumulated in stack print char at passed pointer.

e. Return one after one.

Algorithm working

C++ Program

#include <bits/stdc++.h>

using namespace std;

//Recursive function to reverse a string
void Reverse(char *str)
//Main function
int main()
   char string[] = "code";
   cout<<"Input string: ";
   for (int i = 0; i < strlen(string); ++i)
   cout<<"\nOutput string after reversing: ";
   return 0;

Try It


READ  Repeated Substring Pattern
Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions