Reverse a String in C

Here we will write a C program to find the reverse of a String. The Reverse of String “I am Learning” becomes “gninraeL ma I”.

Reverse a string in C using strrev() function

This function is used to reverse the given string. Reversed string will be placed at the same location. The function strrev() is defined in “string.h”

// reverse of the string
#include<stdio.h>
#include<string.h>
int main()
{
   char str[100];
   printf("Enter string: ");
   fgets(str, sizeof(str), stdin);

   strrev(str);
   printf("The reverse of the string is: ");
   puts(str);

   return 0;
}

Output:-

Enter string: Programming
The reverse of the string is:
gnimmargorP

In the above program, we take input from the user using fgets() function, we can use gets() function also. Then the string is reversed by a predefined function strrev(). Finally, the output is the display. Note that if you are working in the Linux environment then you should know that the strrev() function is not present in the GCC compiler in Linux. You need to make your own function to reverse the string.

Without using String Manipulation Library functions to Reverse

#include<stdio.h>
int main()
{
   char str[100];
   int i, j, temp;
   printf("Enter a string: ");
   scanf("%[^\n]",str);

   // finding length of string
   for(i=0; str[i]!='\0'; i++);

   // reverse
   for(i--, j=0; j<i/2; j++, i--)
   {
     temp=str[i];
     str[i]=str[j];
     str[j]=temp;
   }

   printf("The reverse of the string: %s",str);

   return 0;
}

Output:-

Enter a string: Programming
The reverse of the string: gnigrammorP

In this program, we find the length of string and stored it in the variable i. Then by swapping we reversed the string.

Using Another String

#include<stdio.h>
int main()
{
   char ch[50], a[50], i=-1, j=0;

   printf("Enter the string: ");
   scanf("%[^\n]",ch);

   printf("Reverse is: ");
     
   // finding legth of string
   // and storing it in variable i
   while(ch[++i]!='\0');
     
   // copy the string
   while(i>=0)
   {
     a[j++]=ch[--i];
   }

   a[j++]='\0';

   puts(a);

   return 0;
}

Output:-

Enter the string: awesome
Reverse is: emosewa


Displaying in Reverse Order

#include<stdio.h>
#include<string.h>
int main()
{
   char ch[50];
   int i;

   printf("Enter the string: ");
   scanf("%[^\n]",ch);

   // finding Length of string
   // and storing it into the variable i
   for(i=0; ch[i]!='\0'; i++);

   printf("Reverse is: ");
   for(i--; i>=0; i--)
   {
     printf("%c",ch[i]);
   }

   return 0;
}

Output:-

Enter the string: COMPUTER CORNER
Reverse is: RENROC RETUPMOC

It is not the correct way to reverse the string. In this program, we find the last index (length) of the string and then from last it is displayed.

Reverse a String using Recursion

#include<stdio.h>
void reverse()
{
   char c;
   scanf("%c",&c);

   if(c!='\n')
   {
     reverse();
     printf("%c",c);
   }
}

int main()
{
   printf("Enter a Sentence: ");
   reverse();

   return 0;
 }

Output:-

Enter a Sentence: renroC retupmoC
Computer Corner

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply