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 :
- Input the string
- Find the length of the string
- Swap the position of array element using loop
- Store the swapped position
- 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
Post a Comment