简体   繁体   English

检查字符串是否为回文时出错

[英]Error in checking whether a string is palindrome

I'm trying to make a C program that checks whether a sequence of characters is palindrome or not.我正在尝试制作一个 C 程序来检查字符序列是否为回文。 I must use a stack and a recursive function.我必须使用堆栈和递归函数。

The first number given as input is the length of the string.作为输入给出的第一个数字是字符串的长度。 The program works correctly with even lengths, but doesn't recognize palindromeness of odd strings.该程序可以在偶数长度下正常工作,但无法识别奇数字符串的回文。 I have been trying to find the mistake for at least an hour but no luck.我一直试图找出错误至少一个小时,但没有运气。

This is the code.这是代码。

#include <stdio.h>
#include <stdlib.h>

struct c {
    char cha;
    struct c *nextPtr;
};

typedef struct c Character;

void push(Character **headPtr, char c) {
    Character *newC = malloc(sizeof(Character));

    if(newC == NULL) {
        puts("Insufficient memory.");
    }
    newC->cha = c;
    newC->nextPtr = *headPtr;
    *headPtr = newC;
}

char pop(Character **headPtr) {
    if(*headPtr != NULL) {
        Character *tempPtr = *headPtr;
        char c = (*headPtr)->cha;
        *headPtr = (*headPtr)->nextPtr;
        free(tempPtr);
        return c;
    }
}

void isPalindrome(int stringLength, int charNum) {
    static Character *headPtr = NULL;

    if(stringLength == 1) {
        puts("Palindrome");
        exit(0);
    }
    if(charNum < (stringLength/2)) {
        char c = getchar();
        push(&headPtr, c);
        return isPalindrome(stringLength, charNum+1);
    }
    if(stringLength % 2) {
        getchar();
    }

    char d = getchar();
    char e = pop(&headPtr);
    if(d != e) {
        puts("Not palindrome");
        exit(0);
    }
    return;
}

int main() {
    int n;

    scanf("%d", &n);
    while(getchar()!='\n');
    isPalindrome(n, 0);

    puts("Palindrome.");
    return 0;
}

You can try it yourself by just inputting an odd number, then enter, than a sequence of characters.你可以自己试试,只输入一个奇数,然后回车,而不是一串字符。 The program will not recognize if it's palindrome and always say it's not or even do weirder stuff like not outputting anything at all and waiting for more input.该程序将无法识别它是否是回文,并且总是说它不是,甚至会做一些更奇怪的事情,例如根本不输出任何内容并等待更多输入。

I really don't know what I'm missing.我真的不知道我错过了什么。

Never mind, I found the solution by myself.没关系,我自己找到了解决方案。

#include <stdio.h>
#include <stdlib.h>

int isPal = 1;

struct c {
    char cha;
    struct c *nextPtr;
};

typedef struct c Character;

void push(Character **headPtr, char c) {
    Character *newC = malloc(sizeof(Character));

    if(newC == NULL) {
        puts("Insufficient memory.");
        return;
    }
    newC->cha = c;
    newC->nextPtr = *headPtr;
    *headPtr = newC;
}

char pop(Character **headPtr) {
    if(*headPtr != NULL) {
        Character *tempPtr = *headPtr;
        char c = (*headPtr)->cha;
        *headPtr = (*headPtr)->nextPtr;
        free(tempPtr);
        return c;
    }
}

void isPalindrome(int stringLength, int charNum) {
    if(!isPal) return;

    static Character *stack = NULL;

    if(charNum < (stringLength/2)) {
        char c = getchar();
        push(&stack, c);
        isPalindrome(stringLength, charNum+1);
    } else if(stringLength % 2) {
        getchar();
    }

    char d = getchar();
    char e = pop(&stack);
    if(d != '\n' && d != e) isPal = 0;
}

int main() {
    int n;

    if(scanf("%d", &n) != 1 || n < 0) {
        puts("Incorrect input.");
        return 0;
    }
    while(getchar()!='\n');
    if(n>1) isPalindrome(n, 0);

    if(isPal) puts("Palindrome.");
    else puts("Not palindrome.");
    return 0;
}

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

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