Reverser Function using Recursion

So i have a program it works which takes in a word reverses it prints it out. BUT! the problem is that the program is without recursion. I need help to convert the function into recursion. Please give me some idea or let me know the syntax if you were able to figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;


void Reverse (char *);

int main()
{
    // define array for letters in a word entered
    char word[41];
    
    // ask user for input
    cout << "Enter a word" << endl;
    cin >> word;

    
    Reverse(word);
    
    cout << endl;
    
    system("Pause");
    return 0;
}


void Reverse (char *sentencePtr)

{
    
    char *p = sentencePtr;
    while ( *p != '\0' )
        
        ++p;
    
    while ( p != sentencePtr )
        
        cout.put ( *--p );
    
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void print_reverse( const char* cstr )
{
    if( cstr != nullptr && *cstr != 0 ) // if the first character is not a null character
    {
        const char head = *cstr ; // first character
        const char* tail = cstr+1 ; // rest of the string

        print_reverse(tail) ; // first print the rest of the string in reverse
        std::cout << head ; // and then at the end, print the first character
    }
}
@JLBorges Thank you so much!!
Topic archived. No new replies allowed.