C Program to Reverse a given String

Next Article
The program to reverse a given string takes input string and output reverse of the string. This is program is a simple C program intended for beginners of C language.
The program is written and compiled using Dev-C++ compiler on a windows 7 64-bit system. You can use any other standard C compiler to compile and run the program.
To help you learn this program we have following section – Problem Definition, Flowchart , Source Code for the program and verified Output.

Problem Definition

The program takes an input string at run-time using the builtin function gets( str ) where str is a character array. In this array of characters, the beginning characters are exchanged with the ending characters of the string with the help of a temp variable.
For example,
If the string is “ELEPHANT”
The letter is ‘E’ and ‘T’ are exchanged until all characters are reversed. The final output is TNAHPELE.


C program to reverse a string


Program Code

#include <stdio.h>#include <conio.h>#include <string.h>void main()
{char str[100],temp;int i,j=0;
printf("n Enter the Stringn");
j = strlen(str)-1;while(i<j)
temp = str[i];
str[i] = str[j];
str[j] = temp;
printf("n Reverse String is %s",str);




Output – Reverse the String

Next Article