C Program to Reverse a String

In this C program, we will reverse a string using strrev function, by swapping characters and by using recursion. We first take a string as input from user using gets function and store it in a character array. Now, we have to reverse this string without using any additional character array and print reversed string on screen.

For Example :
Input: TechCrashCourse
Output (Reverse String): esruoChsarChceT
We will show three different approaches to reverse a string.


C program to reverse a string using strrev function

Function strrev reverses the given string and returns a pointer to the reversed string. To use strrev function we must include string.h header file.

char *strrev(char *string);
#include <stdio.h>
#include <string.h>

int main()
{
   char inputArray[100];
 
   printf("Enter a string to reverse\n");
   gets(inputArray);
   
   strrev(inputArray);
   printf("Reversed string is: %s\n",
      inputArray);
      
   return 0;
}
Output
Enter a string to reverse
Hello World
Reversed string is: dlroW olleH

C program to reverse a string by swapping characters inside loop

In this program, we will reverse a string by swapping left and right characters. We will use two integer variables leftIndex and rightIndex and initialize them with position of leftmost and rightmost character in input string.

For Example
If inputString = Apple
leftIndex = 0
rightIndex = 4

Then we swap characters pointed by leftIndex and rightIndex in inputString. After swapping we increment leftIndex and decrement rightIndex to point to next unswapped leftmost and rightmost characters. We continue this process, until leftIndex and rightIndex crosses each other. Here we used for loop but we can similarly use while or do-while loop.

#include <stdio.h>
#include <string.h>

int main()
{
   char inputString[100], temp;
   int length, leftIndex, rightIndex;
   printf("Enter a string to reverse\n");
   gets(inputString);
   
   /* Find length of string */
   length = strlen(inputString);
   leftIndex = 0;
   rightIndex = length -1;
   
   while(leftIndex < rightIndex){
       temp = inputString[leftIndex];
       inputString[leftIndex] = inputString[rightIndex];
       inputString[rightIndex] = temp;
       
       leftIndex++;
       rightIndex--;
   }
   
   printf("Reversed string: %s\n",
      inputString);

   return 0;
}
Output
Enter a string to reverse
Apple
Reversed string: elppA

C Program to reverse a string using recursion

We can use recursion to reverse a string because we can split into sub-problems.

reverse(string, leftIndex, rightIndex) = swap(string, leftIndex, rightIndex) + 
                                         reverse(string, leftIndex+1, rightIndex-1)

To reverse a string of length N using recursion, we have to swap the leftmost and rightmost characters of a string and then recursively reverse the inner sub-string from index 1 to N-2. Keep on repeating this unless size of sub-string is greater than one.

#include <stdio.h>
#include <string.h>

char* reverseString(char *string, int leftIndex, int rightIndex);

int main()
{
   char inputArray[100];
 
   printf("Enter a string to reverse\n");
   gets(inputArray);
   
   reverseString(inputArray, 0, strlen(inputArray) - 1);
   printf("Reversed string\n%s", inputArray);

   return 0;
}


char* reverseString(char *string, int leftIndex, int rightIndex){
      char ch;
      if(NULL == string || leftIndex > rightIndex)
          return NULL;

      ch = string[leftIndex];
      string[leftIndex] = string[rightIndex];
      string[rightIndex] = ch;
      
      reverseString(string, leftIndex + 1, rightIndex - 1);
      return string;
}
Output
Enter a string to reverse
TECHCRASHCOURSE
Reversed string is: ESRUOCHSARCHCET

Related Topics
C program to swap two strings
C program to compare two strings
C program to check string is palindrome
C program to find a substring from a given string
C program to check if two strings are anagram
C program to copy a string
C program to sort characters of a string
List of all C programs