代码不适用于长回文

时间:2013-10-12 23:11:43

标签: c palindrome

我正在进行一项任务,我必须从用户那里输入一个句子,以相反的顺序打印单词,检查字谜,并检查回文。我有一个为anagrams工作的函数,我几乎让我的palindrome函数正常工作。现在,我只要求两个字,所以我可以让我的功能正常工作。然而,出于某种原因,每当我输入一个冗长的回文(ex;赛车或与妈妈或爸爸相比没有分开)我要求的两个单词时,回文函数就会搞砸了。

这是代码;

#include <stdio.h>
#include <ctype.h> //Included ctype for tolower / toupper functions
#define bool int
#define true 1
#define false 0

//Write boolean function that will check if a word is a palindrome
bool palindrome(char a[])
{
    int c=0;
    char d[80];
    //Convert array into all lower case letters
    while (a[c])
    {
        a[c] = (tolower(a[c]));
        c++;
    }
    c = 0;

    //Read array from end to beginning, store it into another array
    while (a[c])
        c++;

    while(a[c] != 0 && c > -1)
    {
        d[c] = a[c];
        c--;
    }

    c = 0;

    while(a[c])
    {
        printf("%c", d[c]);
        printf("%c", a[c]);
        c++;
    }
    //If two arrays are equal, then they are palindromes
    for(c = 0; a[c] && d[c]; c++)
    {
        while(a[c] && d[c])
        {
        if(a[c] != d[c])
            return false;
        }
    }
    return true;
}

int main(void)
{
    char a[80], b[80];
    bool flagp;
    //Prompt user to enter sentence
    printf("Enter a word: ");
    gets(a);

    flagp = palindrome(a);

    if (flagp)
    {
        printf("\nThe word is a palindrome.");
    }
    else
    {
        printf("\nThe word is not a palindrome.");
    }

    return 0;
}

输出此内容;

Enter first word: racecar
_r▬a↨c e c a r
The word is not a palindrome.

但是,如果我输入'赛车',它会错误地说明它不是回文。

请告诉我我做错了什么:'(

2 个答案:

答案 0 :(得分:2)

  1. 因此,a[c] != d[c]如果您认为它是假的,则为真。
  2. 您已使用printf向我们证明这是因为d[c]是垃圾。
  3. 这意味着d不包含a的反向。
  4. 因此,这导致人们检查以下代码段:

    while(a[c] != 0 && c > -1)
    {
        d[c] = a[c];
        c--;
    }
    

    它正在尝试创建一个反向副本,但它显然无法扭转任何看到因为它正在采用相同的索引。

  5. (你做了前三个步骤。你为什么停在那里?)

    老实说,d没有理由存在。它可以全部就地完成。

       +---+---+---+---+---+---+---+
    a: | r | a | c | e | c | a | r |
       +---+---+---+---+---+---+---+
         ^                       ^
         |   compare these two   |
    
    
             ^               ^
             |  then these   |
    
    
                    ...
    

    所以代码看起来像:

    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;
    

    注意:

    1. 请不要#define true 1#define false 0。这些与C的定义不同,因此如果您执行if (b == true)而不是if (b),则可能会得到错误的结果。

    2. c通常表示chari(以及jk)更常用于索引。

答案 1 :(得分:1)

问题出在您的palindrome功能中。在摘录

while(a[c] != 0 && c > -1)
{
    d[c] = a[c];
    c--;
}

您没有撤消a

和摘录

while (a[c])
    c++;  

导致c越过1

我解决了这些问题。您修改过的代码:

bool palindrome(char a[])
{
    int c=0;
    char d[80];
    //Convert array into all lower case letters
    while (a[c])
    {
        a[c] = (tolower(a[c]));
        c++;
    }
    c = 0;

    //Read array from end to beginning, store it into another array
    while (a[c])
        c++; 
        c=c-1; // Number of elements in a is one less than that of counter c.

    int i = 0;  // taking another counter for array d
    while(a[c] != 0 && c > -1)
    {
        d[i] = a[c];
        i++;
        c--;
    }
    d[i] = '\n'; // last element of array mut be a nul char
    c = 0;

    while(a[c])
    {
        printf(" %c\t", d[c]);
        printf(" %c\n", a[c]);
        c++;
    }
    //If two arrays are equal, then they are palindromes
    for(c = 0; a[c] && d[c]; c++)
    {

         if(a[c]  != d[c] )
            return false;

    }
    return true;
}  

毕竟,这个功能不足以检查回文。示例:

输入:

I am a     I

你会得到

The word is not a palindrome.  
相关问题