In this program, the user is allowed to enter words of up to 20 letters, which are stored in another array and printed from the end. But there is error on my code, the program output single letter after I enter a word. That is my code.
#include <stdio.h>
int main(void) {
char user[21]; // created an array and let users enter letters
char reverse[21]; // create an array and store the reversing string
printf("Please enter the word that you want to reverse: \n");
scanf("%20s", user);
int length = sizeof(user) / sizeof(user[0]);
int rever_index = 0;
for (int i = 0; i < length; i++) {
reverse[i] = user[length - i];
rever_index++;
}
printf("reverse value are %s\n", reverse);
return 0;
}
Test result: Please enter the word that you want to reverse
string
reverse value are Z. How to modify my code?