- So
a[c] != d[c]
is true when you expect it to be false. - You've demonstrated with your
printf
that this is becaused[c]
is garbage. - That means that
d
doesn't contain the reverse ofa
. So this leads one to inspect the following snippet:
while(a[c] != 0 && c > -1) { d[c] = a[c]; c--; }
It's attempting to create a reversed copy, but it's quite clearly failing at reversing anything seeing as it's putting at the same index as it's taking.
(You did the first three steps. Why did you stop there?)
Honestly, there's no reason for d
to exists at all. It can all be done in-place.
+---+---+---+---+---+---+---+
a: | r | a | c | e | c | a | r |
+---+---+---+---+---+---+---+
^ ^
| compare these two |
^ ^
| then these |
...
So the code would look like:
size_t len = strlen(a);
if (len) {
size_t i = 0;
size_t j = len - 1;
while (i < j) {
if (a[i++] != a[j--])
return 0;
}
}
return 1;
Notes:
Please don't do
#define true 1
and#define false 0
. These differ from C's definition, so you could get the wrong result if you doif (b == true)
instead ofif (b)
.c
usually denotes achar
.i
(andj
andk
) are more commonly used for indexes.