Question: In c++ make this program to reverse a string use recursion instead of iteration to reverse the string. #include #include using namespace std; string Backwards(string);
In c++ make this program to reverse a string use recursion instead of iteration to reverse the string.
#include
string Backwards(string);
int main() { string s;
cout << "Enter string: "; cin >> s; Backwards(s); }
string Backwards(string s) { int starting = s.length(); int ending = starting - 1; int i = 0;
while(i <= ending) { swap(s[i],s[ending]); ending -= 1; i += 1; }
cout << s; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
