Wednesday 20 July 2016

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;
}

No comments:

Post a Comment

Uploading and Running Lambda function in AWS

Main.go package main import ( "fmt" "encoding/json" "log" "github.com/aws/aws-lambda-g...