Program satement
Write a program to take a input string and reverse it character by character without using string function strrev.
Program to reverse a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> #include <string.h> int main() { char str[50]; int len, i; printf("Enter a string: "); gets(str); len = strlen(str); for(i=len-1; i>=0; i--) { printf("%c", str[i]); } return 0; } |
Output
1 2 |
Enter a string: WebEncyclop polcycnEbeW |
Explanation:
Here in program we store input string into character array a.
By using standard string function strlen we calculate length of a string.
As we know the last index of the string is always ‘\0’, therefore we set index to length-1.
In this way from the last index, we reverse a string and print it.