#include <stdio.h>
#include <string.h>
char *reverse(char *input) {
size_t len = strlen(input);
char output[len];
output[len] = '\0';
for (size_t i = 0; i < len; i++) {
output[len - i - 1] = input[i];
}
input = output;
return input;
}
int main(){
char string1[] = "Hi world, how are you doing, something";
printf("\n");// output differs by commenting this line
printf("%s\n",reverse(string1));
}
The result is wierd. Can anyone explain me why this happens? with the line 16, when it is commented, the output is something garbage value, whereas when a print statment exists before that, expected result is shown
Output when printf("\n"); is commented:
user1@h3:~/code$ ./x1
&&Ќ`we&
user1@h3:~/code$
Output when printf("\n"); is uncommented:
user1@h3:~/code$ gcc x1.c -o x1
user1@h3:~/code$ ./x1
gnihtemos ,gniod uoy era woh ,dlrow iH