Reverse a String in C using For loop
ADVERTISEMENTS
Reverse a string in C using for loop. In this article, you will learn how to reverse a string in c using for loop.
String Conversion
"abcd" after reverse ==> "dcba"
Source Code
// Reverse a String in C using For loop
#include <stdio.h>
#include <string.h>
int main() {
char x[100], y[100];
int b, e, c = 0;
// b - To store the begin
// e - To store the end
// c - To store the count
printf ("Enter a string to make reverse::\n");
scanf ("%s", &x);
// calculation of the string length
while (x[c] != '\0') {
c++;
}
e = c - 1;
for (b = 0; b < c; b++) {
y[b] = x[e];
e--;
}
y[b] = '\0';
printf("The reverse of the string is : %s\n", y);
return 0;
}
Output
Enter a string to make reverse::
example
The reverse of the string is : elpmaxe