C Program to Reverse a String

4 Min Read

In C programming, a string is simply an array of characters which is always terminated by a null character. There are many applications and operation of strings in programming of higher level language. To reverse a string without using the library function is a popular tutorial on string in C programming. In this tutorial post, we are going to discuss C program to Reverse a String along with its algorithm, source code, and sample output.

The basic working principle to reverse a string in C programming is to swap the position of array element i.e. exchange of the position in character array. As the last element in string is a null character, the first element of array is swapped with the second last element of array. The exchanged elements are stored and finally printed as console output.

Algorithm to Reverse a String:

  • Start
  • Declare all the variables ( integer and character type )
  • Enter the string to be reversed
  • Find out the length of string
  • Swap the position of array element using loop
  • Store the swapped position
  • Print the reversed string as console output
  • Stop

Source code to Reverse a String in C:

#include<stdio.h>
#include<string.h>
int main()
{
   char string [100], temp; // declaration of character variables
   int i, j = 0; // declaration of integer variable
   printf(" Enter the string to be reversed:");
   gets(string ); // string input

   i = 0;
   j = strlen(string ) - 1; // counting the length
   while (i < j) // for reversing string
   {
      temp = string [i];
      string [i] = string [j];
      string [j] = temp;
      i++;
      j--;
   }
   printf("\nThe Reversed string is :%s", string ); // Output
   return (0);
}

Above source code for reversing a string  in C includes two header files: stdio.h and string.h. As in other console input/output programs, stdio.h has been included in this program to control fundamental input and output functions such as print(), gets(), scanf() etc. The other header file string.h has been included to utilize strlen() function to find the string length.

As this C program is executed, it asks for the string which is to be revered. As soon as the string is entered, the program finds out the length of the entered string. The length of string is measured using strlen() library function. After that, each array element is swapped using a temporary character variable (temp) and stored. Finally, the reversed string is displayed as output.

Sample Output

Also see,
C Program to Add Two Complex Numbers
Sorting Strings Alphabetically in C
More C Tutorials

The given source code in C program to Reverse a String is short and simple to understand. The source code is well tested in Code::Bolocks IDE and is completely error free.If you have any question or suggestion regarding this post, the algorithm or source code presented it, please bring them up from the comment sections.

Share This Article
2 Comments

Leave a Reply

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

English
Exit mobile version