【问题标题】:C:Checking whether the string is PalindromeC:检查字符串是否为回文
【发布时间】:2016-08-25 06:54:45
【问题描述】:

编写一个检查字符串是否为回文的函数。必须使用递归函数并忽略空格。 我已经完成了第一部分,但仍然不知道如何忽略空间。 以下代码是我已经尝试过的。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>// this is used for strlen
#include <ctype.h>// this is used for isalnum

int checking_palindrome(char *string,int length);
int main()
{
    int result,length;
    char string[] = "a ma ma";
    length = strlen(string);
    result= checking_palindrome(string,length);
    if (result == 1)
        printf("The array is palindrome.\n");
    else
        printf("The array is not palindrome.\n");
    system("pause");
    return 0;
}
int checking_palindrome(char *string, int length)
{
    if (length <= 0)
        return 1;
    else if (string[0] == string[length-1])
        return checking_palindrome(&string[0], length - 2);
    else
        return 0;
}

【问题讨论】:

  • 你为什么使用&amp;string[0]而不是&amp;string[1]或string + 1作为checking_palindrome()的第一个参数?
  • 了解如何使用调试器,以及如何在监控变量及其值的同时逐行执行代码。那会很快帮助你。对于任何想要更认真地对待任何类型的编程的人来说,这也是一项至关重要的技能,即使对于业余爱好者也是如此。
  • @MikeCAT 实际上,这是我的错误。我的原始代码是 &string[1].
  • @JoachimPileborg 谢谢你的建议。

标签: c string pointers palindrome


【解决方案1】:

要忽略空格,请编写代码忽略空格。

int checking_palindrome(char *string, int length)
{
    if (length <= 0)
        return 1;
    else if (string[0] == ' ')
        /* ignore space at the head of the string */
        return checking_palindrome(&string[1], length - 1);
    else if (string[length-1] == ' ')
        /* ignore space at the tail of the string */
        return checking_palindrome(&string[0], length - 1);
    else if (string[0] == string[length-1])
        /* Respecting original code: &string[0] is used instead of &string[1] for some reason */
        return checking_palindrome(&string[0], length - 2);
    else
        return 0;
}

【讨论】:

  • 尊重原始代码是不明智的,因为这是原始代码的缺陷之一。我感觉 OP 使用 amama 进行了测试,因此将长度减少 2 会导致 ama。但是代码仍然是错误的。另外,第一个if 可能是if(length&lt;=1)。
  • 另外,如果部分回文比不匹配的空格更有可能发生,那么更早地进行相等检查,在长度检查之后将是一个胜利。
【解决方案2】:

在得到我老师的建议后,最好从数组中删除空格并将值放入新数组。然后,将新数组传递给函数。

#include "stdafx.h"//Visual Studio 2015
#include <stdio.h>
#include <stdlib.h>// using for command system("pause") ;
#include <string.h>


int checking_palindrome(char *string, int length);
int main()
{
    int i,j,result, length;
    char string[] = "a ma ma",newstring[50];

    length = strlen(string);
    for(i=0,j=0;i<length;i++)
        if (string[i] != ' ')
        {
            newstring[j] = string[i];
            j++;
        }
    newstring[j] = '\0';
    length = strlen(newstring);

    result = checking_palindrome(newstring, length);
    if (result == 1)
        printf("The array is palindrome.\n");
    else
        printf("The array is not palindrome.\n");
    system("pause");
    return 0;
}
int checking_palindrome(char *string, int length)
{
    if (length <= 0)
        return 1;
    else if (string[0] == string[length - 1])
        return checking_palindrome(&string[1], length - 2);
    else
        return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 2015-10-02
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多