Reverse a String (Recursive) C++

C++Server Side ProgrammingProgramming

Recursion is simply the way toward rehashing things in a self-comparative way. In programming dialects, if a program enables you to call a capacity inside a similar capacity, at that point, it is known as a recursive call of the capacity. You can switch a string utilizing the recursive capacity as appeared in the accompanying project.

Example

 Live Demo

#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 = "Arnold";
   cout<<"recursive reverse (Arnold) ::";
   reverse(a);
   return 0;
}

Output

The above C++ program accepts a string "Arnold" as an input then it yields results as "dlonrA" by applying recursion as;

Recursive reverse (Arnold):: dlonrA
raja
Published on 23-Dec-2019 06:30:31
Advertisements