C++ program to reverse a given string using recursion
codeaft.cpp
#include<iostream>usingnamespacestd;voidreverse(string&s,intl,inth){if(l<h){swap(s[l],s[h]);reverse(s,l+1,h-1);}}intmain(){cout<<"———————————————————————————————————————————";cout<<"\nProgram to reverse a given string";cout<<"\n———————————————————————————————————————————";strings;cout<<"\nEnter the string ";cin>>s;reverse(s,0,s.length()-1);cout<<"\nThe reverse string is "<<s;cout<<"\n———————————————————————————————————————————\n";}
Output
codeaft@codeaft:~$ g++ codeaft.cpp codeaft@codeaft:~$ ./a.out
———————————————————————————————————————————
Program to reverse a given string
———————————————————————————————————————————
Enter the string Codeaft
The reverse string is wodniWgnidoK
———————————————————————————————————————————
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.