C Program to reverse a string

               C PROGRAM TO REVERSE A STRING


Consider the following I/O samples to reverse a string.


SAMPLE INPUT: techtrendstract

SAMPLE OUTPUT: tcartsdnerthcet


The algorithm used here is :

  1. Input the string
  2. Find the length of the string
  3. Swap the position of array element using loop
  4. Store the swapped position
  5. Print the reversed string.  

C PROGRAM:  

#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);

}


OUTPUT:



                     

The given source code in C program to Reverse a String is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. 

If you have any feedback about this article and want to improve this, please write to                                                                nivisonu23@gmail.com


Comments

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

Design a way to sort the list of positive integers in the descending order according to frequency of elements. The elements with integers with higher frequency come before with lower frequency elements with same frequency come in the same order as they appear the values.