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. The 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. See more:- Different ways to read and write String in C

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.

Reverse a String in C without using String Manipulation Library functions

In the previous program to reverse a string in C using strrev(), we used a pre-defined String manipulation function strrev(). But now, let us develop another program without using any string manipulation library functions.

Procedure to find reverse of string in C without using the pre-defined function,

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

1) Find the length of the string.
2) Iterate through the string using loop
3) Swap first and last index characters and so on till the middle of the string.
4) Display the string.

Example:- If the String = “know” then the character ‘k’ will be swapped with character ‘w’, and character ‘n’ will be swapped with the character ‘o’.

#include<stdio.h>
int main()
{
   char str[100];
   int len, i, j;
   char ch;

   // read string
   printf("Enter a string: ");
   scanf("%[^\n]",str);

   // finding length of string
   for(len=0; str[len]!='\0'; len++);
   i = len - 1; // actual length by 0 index

   // reverse the string
   for(int j=0; j < len/2; j++, i--)
   {
     ch = str[i];
     str[i] = str[j];
     str[j] = ch;
   }

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

   return 0;
}

Output:-

Enter a string: KnowProgram
The reverse of the string: margorPwonK

Enter a string: Learn C Programming
The reverse of the string: gnimmargorP C nraeL

Reverse a String in C using Another String

In the previous program, we used the same string to reverse a string in C. But we can also take the help of another string. This approach will be very easy compared to the previous program.

Procedure to reverse a string in C using another String,

1) Find the length of the String.
2) Take a string of appropriate size so that it can store the reverse value.
3) Pick characters from the end of the original string and fill them from the front end of the new String.
4) Insert the ‘\0’ character at the end of the new string.
5) Display the new 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 in C 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 Comment

Your email address will not be published. Required fields are marked *