
Originally Posted by
pankaj401
I would suggest that you reverse the string in place and then print it with "%s" format specifier. That way you save multiple calls to printf.
Thank you so much for your suggestion. I have made some changes to my code taking your suggestion:
Code:
//This is a small program to reverse the string provided by user
#include<stdio.h>
#include<string.h>
int main() {
char string[256], revstr[256];
int len, i;
printf("Enter a string:\n");
fgets(string, 256-1, stdin);
len = (strlen(string) - 1);
printf("You entered %s \nlength %d:\n", string, len);
i = len;
printf("Reverse string:");
for(; len >= 0; len--)
revstr[i-len] = string[len];
revstr[i+1] = '\0';
printf("Reversed string is: %s\n", revstr);
exit(0);
}