编写一个函数来检查字符串是否是回文。必须使用递归函数并忽略空格。 我已经完成了第一部分,但仍未弄清楚如何忽略空间。 以下代码是我已经尝试过的。C:检查字符串是否为Palindrome
#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;
}
为什么您使用'&string [0]'而不是'&string [1]'或者'string + 1'作为'checking_palindrome()'的第一个参数? – MikeCAT
了解如何使用调试器,以及如何在监控变量及其值的同时逐行执行代码。这会很快帮助你。对于任何想要更严肃地进行编程的人来说,这也是一项关键技能,即使对于业余爱好程序员也是如此。 –
@MikeCAT其实,那是我的错。我的原始代码是&string [1]。 –