C Program to Reverse a String using Recursion

This is a C Program to reverse the string using recursion.

Problem Description

This C Program reverses the string using recursion.

Problem Solution

This C Program uses recursive function & reverses the string entered by user in the same memory location. Eg: “program” will be reversed to “margorp”

Program/Source Code

Here is the source code of the C program to reverse a string. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Reverse the String using Recursion
 */
#include <stdio.h>
#include <string.h>
 
void reverse(char [], int, int);
int main()
{
    char str1[20];
    int size;
 
    printf("Enter a string to reverse: ");
    scanf("%s", str1);
    size = strlen(str1);
    reverse(str1, 0, size - 1);
    printf("The string after reversing is: %s\n", str1);
    return 0;
}
 
void reverse(char str1[], int index, int size)
{
    char temp;
    temp = str1[index];
    str1[index] = str1[size - index];
    str1[size - index] = temp;
    if (index == size / 2)
    {
        return;
    }
    reverse(str1, index + 1, size);
}
Program Explanation

In this C program we are reading a string using ‘str1[]’ array variable. Assign the value of the length of string using strlen() to size variable.

advertisement

If condition statement is used to check that both the values of ‘index’ and ‘size’ variables are equal and divide the value by 2. If the condition is true then execute the statement and return the value.

Otherwise, if the condition is false then exit the statement. Again call the reverse() function by passing the value of ‘str1’ variable and the summation of the value of ‘index’ variable with 1 and the value of ‘size’ variable as argument. Print the reversed string from the given string.

Runtime Test Cases
 
$ cc pgm12.c
$ a.out
Enter a string to reverse: malayalam
The string after reversing is: malayalam
 
$ a.out
Enter a string to reverse: cprogramming
The string after reversing is: gnimmargorpc

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.