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
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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate 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
If you find any mistake above, kindly email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.