Signup/Sign In

Program to Reverse a String Using Recursion

Below is a program to reverse a user input string using recursion in C language.


// declaring recursive function
char* reverse(char* str);

void main()
    int i, j, k;
    char str[100];
    char *rev;
    printf("Enter the string:\t");
    scanf("%s", str);
    printf("The original string is: %s\n", str);
    rev = reverse(str);
    printf("The reversed string is: %s\n", rev);

// defining the function
char* reverse(char *str)
    static int i = 0;
    static char rev[100];
        rev[i++] = *str;
    return rev;

Enter the string: studytonight The original string is: studytonight The reverse string is: thginotyduts