C++ program to reverse a given string using recursion
kw.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
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:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.