I'm new to C, and I've been given a question to write a program to check palindrome words. I did the following code, it gives an output. but the output is always "No". The idea of what I did in this code is, I first divided the string and pushed them into one stack (stacka). then pushed the rest of the letters to another stack(stackb). Then I pop both of those stacks and check whether the letter returning from each pop function(of stacka and stackb) is equal or not. if not it will return 0. below is the code. Thank you in advance. Have a nice day!.
#include <stdio.h>
#include <string.h>
char stacka[5];
char stackb[5];
int topa = -1;
int topb = -1;
void pusha(char e) {
topa++;
stacka[topa] = e;
}
void pushb(char e) {
topb++;
stackb[topb] = e;
}
char popa() {
char e = stacka[topa];
topa--;
return e;
}
char popb() {
char e = stackb[topb];
topb--;
return e;
}
int palindrome(char str[]) {
int i, length = strlen(str);
int mid = length / 2;
for (i = 0; i < mid; i++) {
pusha(str[i]);
}
if (length % 2 != 0) {
i++;
}
for (i = length - 1; i >= mid; i--) {
pushb(str[i]);
}
int f;
for (f = mid; f >= 0; f--) {
char ele1 = popa();
char ele2 = popb();
if (ele1 != ele2)
return 0;
}
return 1;
}
int main() {
char str[] = "madam";
if (palindrome(str)) {
printf("Yes");
} else
printf("No");
}