Write C++ program to Reverse a String Using Recursion
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; void reverse(const string& a); int main() { string str; cout << " Please enter a string " << endl; getline(cin, str); reverse(str); return 0; } void reverse(const string& str) { size_t numOfChars = str.size(); if(numOfChars == 1) cout << str << endl; else { cout << str[numOfChars - 1]; reverse(str.substr(0, numOfChars - 1)); } } |

Post a Comment