Reverse in Place
Completed
Read a word and reverse it in place by swapping characters from both ends, then print it.
Input
One word.
Output
The reversed word.
Example
Input
hello
Output
olleh
Editor
C
Hints
Two indexes walk towards the middle, swapping as they go.
Solution explanation
The two-index technique reverses without a second buffer.
Show solution
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
scanf("%s", s);
int i = 0, j = strlen(s) - 1;
while (i < j) {
char t = s[i];
s[i] = s[j];
s[j] = t;
i++;
j--;
}
printf("%s\n", s);
return 0;
}