C++ program to reverse a given string using recursion
kw.cpp
#include <iostream>
using namespace std;
void reverse(string &s, int l, int h)
{
    if (l < h)
    {
        swap(s[l], s[h]);
        reverse(s, l + 1, h - 1);
    }
}

int main()
{
    cout<<"———————————————————————————————————————————";
    cout<<"\nProgram to reverse a given string";
    cout<<"\n———————————————————————————————————————————";
    string s;
    cout<<"\nEnter the string ";
    cin>>s;
    reverse(s, 0, s.length() - 1);
    cout<<"\nThe reverse string is "<<s;
    cout<<"\n———————————————————————————————————————————\n";
}
Output
kodingwindow@kw:~$ g++ kw.cpp
kodingwindow@kw:~$ ./a.out ——————————————————————————————————————————— Program to reverse a given string ——————————————————————————————————————————— Enter the string Kodingwindow The reverse string is wodniwgnidoK ——————————————————————————————————————————— kodingwindow@kw:~$
Advertisement