I'm trying to create a function that reverses a char *
. This is what I have so far:
#include <stdio.h>
#include <string.h>
char *reverse(char *x) {
int len = strlen(x);
char ans[len+1];
int i;
for (i = 0; i < len; i++) {
ans[i] = x[len-i-1];
}
ans[i] = '\0';
return ans;
}
int main() {
char *a = reverse("hello");
printf("%s\n", a);
}
It should print olleh
, but for me, nothing prints out. Does anyone know where I messed up?