-1

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");
}
pradeexsu
  • 1,029
  • 1
  • 10
  • 27
  • 2
    What debugging have you tried, and what was the result? – DarthQuack Dec 22 '20 at 07:08
  • @a.Li the result is always the same, it would return yes or no every time. but the answer is wrong. – Bhagya Amarasinghe Dec 22 '20 at 07:14
  • Sorry, but this sounds a bit too convoluted. Why don't you just *compare* the chars, starting from the extremes, is it a requirement of the assignment? – Bob__ Dec 22 '20 at 07:14
  • Is there a reason you chose to use a stack? That is doable, but a bit convoluted. You may find [How to remove spaces and check if a string is a palindrome?](https://stackoverflow.com/a/55414017/3422102) shorter if that is of interest. – David C. Rankin Dec 22 '20 at 07:27
  • 1
    Use [GCC](http://gcc.gnu.org/) as `gcc -Wall -Wextra -g` then use [GDB](https://www.gnu.org/software/gdb/) to understand the behavior of your executable – Basile Starynkevitch Dec 22 '20 at 07:28
  • 2
    You really need to actually run it under a debugger. Read on how to set it up on your computer, or use e.g. onlinegdb. Debugging isn't just running your program and seeing the results - there's a tool called a debugger that lets you see the state of the program from "inside" rather than outside (just looking at the printed results). – Kuba hasn't forgotten Monica Dec 22 '20 at 07:40

3 Answers3

0

At a first glance, I have found the following issues in your code.

if(length % 2 !=0)
{
    i++;
}
for(i=length-1;i>=mid;i--)
{
    pushb(str[i]);
}

Your if block does not make any impact on the code and it is unnecessary as well. for loop has an issue as well which can cause your problem.

Let me elaborate a bit. Let's say your input is "madam" and according to your solution, first for loop is doing ok.

int mid=length/2;
for(i=0;i<mid;i++)
{
    pusha(str[i]);
}

As your input is "madam" your value for mid would be 2. so, the first stack will contain the letter, 'm', 'a'. This is alright up to this.

In the second for loop there is a issue,

for(i=length-1;i>=mid;i--)
{
    pushb(str[i]);
}

According to this stack will contain 'm', 'a', 'd'.

Now I would say the correction here according to me. First of all, you remove the first if block, then do this modification on the second for loop.

for(i=length-1;i>mid;i--)
{
    pushb(str[i]);
}

Then finally in last for loop,

for(f=mid-1;f>=0;f--)
{
   char ele1=popa();
   char ele2=popb();
   if(ele1!=ele2)
     return 0;
}

I have not compiled and run the code with my given corrections. But I think this will help you to figure out the issues with your code.

Shovo
  • 133
  • 2
  • 9
  • There are several problems with the code, but this answer only pointed out one of the them. Removing the if statement doesn't make the program work. – RabidBear Dec 22 '20 at 07:23
-1

தலைவா எனக்கு ஆங்கிலம் நல்லா வராது.

First u change the condition of for loop in palindrome function.

for(i=length-1;i>mid;i--)
{
    pushb(str[i]);
}

Your program is correct.

Thangavel
  • 7
  • 2
-1

First u change the condition of for loop in palindrome function.

for(i=length-1;i>mid;i--)
{
    pushb(str[i]);
}
Thangavel
  • 7
  • 2