Reverse String using Recursion C++


Here the Logic is simple to print the last character of the string for each function call and again call the reverse function again ,this time the string doesn't contain last character.


#include<bits/stdc++.h>

using namespace std;

void reve(string k)
{

  int sz=k.size();
  if(sz==1)
    cout<<k[sz-1];
  else
  {
     cout<<k[sz-1];
     string p=k.substr(0,sz-1);
     reve(p);
  }
}

int main()
{
    string s;
    cin>>s;
    reve(s);

    return 0;
}

Comments

Popular posts from this blog

Divide Intervals Into Minimum Number of Groups

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )