Coding_master_guy's blog

By Coding_master_guy, 3 years ago, In English

i want to recursively reverse a string in c++ but this code only swaps first and last characters of the string and leave other characters as it is

#include <iostream>
#include<string>

using namespace std;

string reverse(string s, int start,int end)
{
    if(start < end)
    {
        swap(s[start] , s[end]);
        reverse(s , start +1 , end -1);
        
    }

     return s;
    
}

int main()
{
   string s;
   cin>>s;
   
   cout<<reverse(s,0,(s.length() -1));

    return 0;
}

Full text and comments »