1

我正在处理一项任务,我必须从用户那里获取句子输入,以相反的顺序打印单词,检查字谜,并检查回文。我有一个适用于字谜的函数,而且我的回文函数几乎可以正常工作。现在,我只要求两个词,这样我就可以让我的功能正常工作。然而,出于某种原因,每当我为我要求的两个词输入一个冗长的回文(例如;racecar 或与妈妈或爸爸相比脱发)时,回文功能就会变得混乱。

这是代码;

#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.

但是,如果我输入“racecar”,它会错误地指出它不是回文。

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

4

2 回答 2

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--;
    }
    

    它试图创建一个反向副本,但很明显它在反转任何东西时都失败了,因为它与它所采用的索引相同。

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

老实说,根本没有存在的理由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通常表示 a chari(and jand k) 更常用于索引。

于 2013-10-12T23:57:49.923 回答
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.  
于 2013-10-13T00:24:33.150 回答