Signup/Sign In

C Program To Reverse The String Using Recursion

Learn C language tutorial

Logic To Reverse The String Using Recursion:

  • Get the input string from the user,
  • The if condition is used to check the index value and str value if both are equal returns the value,
  • else exit the loop and again calls the rev function once the condition is satisfied returns the reverse string as output,

Program To Reverse The String Using Recursion:

#include <stdio.h>
#include <string.h>
 
void rev(char [], int, int);
int main()
{
    char str[50];
    int value;
 
    printf("Enter The String: ");
    scanf("%s", str);
    value = strlen(str);
    rev(str, 0, value - 1);
    printf("The String After Reversing: %s\n", str);
    return 0;
}
 
void rev(char str[], int index, int value)
{
    char temp;
    temp = str[index];
    str[index] = str[value - index];
    str[value - index] = temp;
    if (index == value / 2)
    {
        return;
    }
    rev(str, index + 1, value);
}

Output:

Reverse