Reputation: 13
The program is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
setbuf(stdout,NULL);
int l,i;
char s[10],c;
printf("Enter the string: ");
gets(s);
l=strlen(s);
for(i=0;i<l/2;i++){
c=s[i];
s[i]=s[l-1-i];
s[l-1-i]=c;
}
printf("The reversed string is: %s ",s);
return EXIT_SUCCESS;
}
The output is: Enter the string: hello world. The reversed string is: hlrow olleo.
Upvotes: 0
Views: 58
Reputation: 1
just try to increase the size of array of character bcoz the size of input string "hello world" is greater than 10 OR try to insert small string like "hi world"
Upvotes: 0
Reputation: 645
First of all, as mentioned by @Passerby, the gets function should no longer be used. Instead, you can use the fgets() function to take the input.
fgets(s, sizeof(s), stdin); // read string
Secondly, you are storing your input in an array of size 10, whereas your input string is of length 11. So, the 'd' of 'world' is not read at all. Change the array size to a bigger value and replace gets with fgets, and the problem will be fixed.
char s[15], c;
Upvotes: 2