fastread homefastrread library fastread menu

C++ : Reverse a Sentence Using Recursion

Tutorial by:Manisha Dubey      Date: 2016-06-18 04:51:04

❰ Previous Next ❱

This program takes a sentence from user and reverses that sentence using recursion.

Source code to reverse a sentence using recursion.


/* Example to reverse a sentence entered by user. */

#include <iostream>
#include <string>
using namespace std;
void reverse(const string& a);
int main()
{
    string a;
    cout << " Please enter a string " << endl;
    getline(cin, a);
    reverse(a);
    return 0;    
}
void reverse(const string& a)
{
    size_t n = a.size();
    if(n == 1)
       cout << a << endl;
    else
    {
       cout << a[n-1];
       string b = a.substr(0, n-1);
       reverse(b);
    }    
}

Output

Enter a sentence: margorp emosewa
awesome program

❰ Previous Next ❱


C++

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website