简体   繁体   English

使用 C 中的堆栈检查回文

[英]Check for palindrome using stack in C

I'm new to C, and I've been given a question to write a program to check palindrome words.我是 C 的新手,我被要求编写一个程序来检查回文词。 I did the following code, it gives an output.我做了以下代码,它给出了 output。 but the output is always "No".但 output 始终为“否”。 The idea of what I did in this code is, I first divided the string and pushed them into one stack (stacka).我在这段代码中所做的想法是,我首先划分字符串并将它们推入一个堆栈(stacka)。 then pushed the rest of the letters to another stack(stackb).然后将字母的 rest 推到另一个堆栈(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.然后我弹出这两个堆栈并检查从每个弹出函数(stacka 和 stackb)返回的字母是否相等。 if not it will return 0. below is the code.如果不是,它将返回 0。下面是代码。 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");
}

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.您的if块不会对代码产生任何影响,它也是不必要的。 for loop has an issue as well which can cause your problem. for loop也有一个问题,这可能会导致您的问题。

Let me elaborate a bit.让我详细说明一下。 Let's say your input is "madam" and according to your solution, first for loop is doing ok.假设您的输入是“女士”,根据您的解决方案,第一个 for 循环运行正常。

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 .由于您的输入是“女士”,因此mid的值为2 so, the first stack will contain the letter, 'm', 'a'.因此,第一个堆栈将包含字母“m”、“a”。 This is alright up to this.这没关系。

In the second for loop there is a issue,在第二个for loop中有一个问题,

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

According to this stack will contain 'm', 'a', 'd'.根据这个堆栈将包含'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 .首先,您删除第一个if块,然后对第二个for loop进行此修改。

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

Then finally in last for loop ,然后最后在最后一个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.但我认为这将帮助您找出代码中的问题。

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

First u change the condition of for loop in palindrome function.首先你改变回文function中for循环的条件。

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

Your program is correct.你的程序是正确的。

First u change the condition of for loop in palindrome function.首先你改变回文function中for循环的条件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM