In this piece of code i wanted to scan a word and return it backwards. I tried using different methods, but i dont seem to make it work.
This is what I am now left with.
#define WORD_LENGTH 256
char * stringBackwards(char *a) {
char *palindrom = malloc(WORD_LENGTH * sizeof(char));
int k = 0;
for ( int i = WORD_LENGTH; i >= 0; i--, k++) {
palindrom[k] = a[i];
}
return palindrom;
}
main() {
char word[WORD_LENGTH];
printf("Please enter a word \n");
scanf_s("%s", word, WORD_LENGTH);
printf("%s", stringBackwards(word));
return 0;
}
It always returns me some strange symbols in my console.
I hope some of you can help me in correcting this.