C++ Program to Reverse String using Recursion

C++ Program to Reverse String using Recursion

Write C++ Program to Reverse String using Recursion

// CPP Program to Reverse String using Recursion

#include <iostream>
#include <string.h>

using namespace std;

void reverse_string(char S[], int i, int l)
{
    char temp;
    temp = S[i];
    S[i] = S[l - i];
    S[l - i] = temp;
    if(i == l / 2)
    {
        return;
    }
    reverse_string(S, i + 1, l);

}
int main()
{
	char S[50];
	cout << "Enter String :--> ";
	gets(S);

	int l = strlen(S);
	reverse_string(S, 0, l - 1);
	cout << "Reverse String :--> " << S;

	return 0;
}

Output:

Enter String :--> CodeCrucks
Reverse String :--> skcurCedoC