C Program to reverse string using while
Program
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char inputString[MAX_SIZE], reverseString[MAX_SIZE];
int strIndex, revIndex, length;
length = 0;
printf("Enter any string: ");
scanf("%s", inputString);
while(inputString[length] != '\0')
{
length++;
}
revIndex = 0;
strIndex = length - 1;
while(strIndex >= 0)
{
reverseString[revIndex] = inputString[strIndex];
strIndex--;
revIndex++;
}
reverseString[revIndex] = '\0';
printf("Original string: %s\n", inputString);
printf("Reverse string: %s\n", reverseString);
return 0;
}
Output
$ gcc reverse-string-using-while.c
$ ./a.out
Enter any string: example
Original string: example
Reverse string: elpmaxe