[英]C recursive function palindrome
I'm trying to do a recursive function with C to check if a word is a palindrome or not (read in both ways). 我正在尝试使用C做一个递归函数,以检查一个单词是否是回文(以两种方式读取)。
It's the first time I use this kind of function, but I have a problem, I don't know why it doesn't work, if you could help me, there's my code, thanks : 这是我第一次使用这种功能,但是我有一个问题,我不知道为什么它不起作用,如果可以的话,有我的代码,谢谢:
#include <stdio.h>
#include <string.h>
int palindrome(char c[100],int i, int j)
{
if (j == i)
{
return 1;
}
// Si le premier et le dernier caractère
// sont les mêmes alors, on peut commencer les tests
if(c[i] == c[j])
{
// On fais les tests pour chaque caractère de la chaine
return palindrome(c, i++, j--);
} else {
return 0;
}
return 0;
}
int main(void)
{
char chaine[100] = "radar";
int pal;
pal = palindrome(chaine, 0, strlen(chaine)); // Returns : 0 -> False / 1 -> True
printf("%d", pal);
return 0;
}
The problem is your passing j value as strlen(chaine) instead of strlen(chaine) - 1. 问题是您将j值传递为strlen(chaine)而不是strlen(chaine)-1。
#include <stdio.h>
#include <string.h>
int palindrome(char c[100],int i, int j) {
if (j == i)
return 1;
else if(c[i] == c[j])
return palindrome(c, ++i, --j);
else
return 0;
}
int main() {
char chaine[100] = "radar";
int pal;
pal = palindrome(chaine, 0, strlen(chaine) -1 ); // Returns : 0 -> False / 1 -> True
printf("%d", pal);
return 0;
}
If you can achieve what you are looking for without using recursion do the following. 如果不使用递归就可以实现所需的结果,请执行以下操作。 Read the word into a list on and then reverse the word into another list and if they match you have a palindrome.
将单词读入列表中,然后将其反转到另一个列表中,如果它们匹配,则说明您有回文。
Modified code : 修改后的代码:
1) If first index is 0 , then last index should be strlen(chaine) - 1. 1)如果第一个索引为0,则最后一个索引应为strlen(chaine)-1。
2) You were passing same values of i and j in the recursive call , you need to use pre-increment and post-increment operators. 2)您在递归调用中传递了相同的i和j值,您需要使用pre-incre和post-increment运算符。
#include <stdio.h>
#include <string.h>
int palindrome(char c[100],int i, int j)
{
if (j == i)
{
return 1;
}
if(c[i] == c[j]) // Si le premier et le dernier caractère sont les mêmes alors, on peut commencer les tests
{
// On fais les tests pour chaque caractère de la chaine
// increment and decrement first
// You were passing same values every time (0 and 4)
return palindrome(c, ++i, --j);
}
else
{
return 0;
}
return 0;
}
int main()
{
char chaine[100] = "radar";
int pal;
// Last index should be strlen(chaine) - 1
pal = palindrome(chaine, 0, strlen(chaine)-1); // Returns : 0 -> False / 1 -> True
printf("%d", pal);
return 0;
}
You have several problems with your recursion. 您的递归有几个问题。 First,
i++
and j--
pass the values of i
and j
with the post increment/decrement applied as a side effect after the next call to palindrome
has already been made. 首先,
i++
和j--
在下一次调用palindrome
调用后,将i
和j
的值传递给副作用 ,并应用后增加/减少。 Instead you need: 相反,您需要:
return palindrome (c, i + 1, j - 1);
Next, what happens if chaine
has an even number of characters? 接下来,如果
chaine
的字符数为偶数,该怎么办? i
never equals j
. i
从不等于j
。 You need to update your test to break recursion to: 您需要更新测试以中断递归至:
if (j <= i)
return 1;
Lastly, you are attempting to read the nul-terminating character when you call palindrome
in main()
using strlen(chaine)
, instead you need: 最后,当您使用
strlen(chaine)
在main()
调用palindrome
时,您尝试读取n 终止字符,而您需要:
pal = palindrome (chaine, 0, strlen (chaine) - 1);
Putting that together, you could do something like: 放在一起,您可以执行以下操作:
#include <stdio.h>
#include <string.h>
int palindrome (const char *c, int i, int j)
{
if (j <= i)
return 1;
if (c[i] == c[j])
return palindrome (c, i + 1, j - 1);
return 0;
}
int main (int argc, char **argv) {
char *chaine = argc > 1 ? argv[1] : "radar";
int pal;
pal = palindrome (chaine, 0, strlen (chaine) - 1);
printf ("chaine : '%s' %s a palindrome\n", chaine, pal ? "is" : "is not");
return 0;
}
Example Use/Output 使用/输出示例
$ ./bin/recursive_palindrome
chaine : 'radar' is a palindrome
$ ./bin/recursive_palindrome amanaplanacanalpanama
chaine : 'amanaplanacanalpanama' is a palindrome
$ ./bin/recursive_palindrome catfish
chaine : 'catfish' is not a palindrome
(for the curious, it is "a man a plan a canal panama" ) (出于好奇,是“男人计划运河巴拿马” )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.