为什么递归“停止”?

发布于 2025-02-04 19:41:32 字数 1437 浏览 1 评论 0原文

我试图理解递归。 我的老师编写了这个程序,以确定字符串是否为文接受。

#include <stdio.h>
#include <string.h>
#define SIZE 80

bool palindrome( const char * const sPtr ); // prototype
bool palindromeKernel(const char * const sPtr, size_t l, size_t r);

int main( void ) {
   char sentence[ SIZE ]; 
   puts( "Enter a line of text:" );

   fgets( sentence, SIZE, stdin );

   puts( "\nIs the line palindrome?" );

   if ( palindrome( sentence )) printf("yes");
   else printf("no");
   putchar('\n');
    } 

bool palindrome( const char * const sPtr ){
   size_t len = strlen(sPtr);
   return(palindromeKernel(sPtr, 0, len-2)); 
}

bool palindromeKernel(const char * const sPtr, size_t l, size_t r){
   puts("1");
   if (l >= r) return true;
   return ((sPtr[l] == sPtr[r]) && palindromeKernel(sPtr, l+1, r-1));
}

我添加了一个printf语句,以检查何时调用palindromekernel。当我输入腔室时,它可以按照我的期望工作:

Output 1
Enter a line of text:
mrowlatemymetalworm

Is the line palindrome?
1
1
1
1
1
1
1
1
1
1
yes

这里称为10次,我理解原因。我不明白的是:

mtowlatemymetalworm

Is the line palindrome?
1
1
no

字符串不是回文(我修改了第二个字符)。 在这里,该函数称为2次,因此据我了解,它在返回false时停止自调用。 为什么?

编辑

好的,非常感谢。 但是现在我有一个疑问。

但是,如果我将最后一个回报的顺序倒置,那么这样:

return (palindromeKernel(sPtr, l+1, r-1) && (sPtr[l]==sPtr[r]));

程序仍然有效 为何如此? 我真的不明白递归功能在if条件下如何起作用

I'm trying to understand recursion.
My teacher wrote this program to determine whether a string is a palindrome or not.

#include <stdio.h>
#include <string.h>
#define SIZE 80

bool palindrome( const char * const sPtr ); // prototype
bool palindromeKernel(const char * const sPtr, size_t l, size_t r);

int main( void ) {
   char sentence[ SIZE ]; 
   puts( "Enter a line of text:" );

   fgets( sentence, SIZE, stdin );

   puts( "\nIs the line palindrome?" );

   if ( palindrome( sentence )) printf("yes");
   else printf("no");
   putchar('\n');
    } 

bool palindrome( const char * const sPtr ){
   size_t len = strlen(sPtr);
   return(palindromeKernel(sPtr, 0, len-2)); 
}

bool palindromeKernel(const char * const sPtr, size_t l, size_t r){
   puts("1");
   if (l >= r) return true;
   return ((sPtr[l] == sPtr[r]) && palindromeKernel(sPtr, l+1, r-1));
}

I added a printf statement to check when the palindromeKernel is called. When I input a palindrome it works as I expect:

Output 1
Enter a line of text:
mrowlatemymetalworm

Is the line palindrome?
1
1
1
1
1
1
1
1
1
1
yes

Here the function is called 10 times, and I understand why. What I don't understand is this:

mtowlatemymetalworm

Is the line palindrome?
1
1
no

The string is not a palindrome (I modified the second character).
Here the function is called 2 times, so from what I understand it stops calling itself when it returns false.
Why?

EDIT

Alright thank you so much.
but now I have another doubt.

But if I invert the order of the last return, like this:

return (palindromeKernel(sPtr, l+1, r-1) && (sPtr[l]==sPtr[r]));

the program still works
How so?
I don't really understand how a recursive function work inside an if condition

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-02-11 19:41:32

这线:

return ((sPtr[l] == sPtr[r]) && palindromeKernel(sPtr, l+1, r-1));

是诀窍。 C具有所谓的“短路评估”。和||,因此,如果AN&amp;&amp;表达式为false,它不需要评估第二个,因此当两个字符与不匹配(sptr [l] == sptr [r]为false)时,它会递归地呼叫自己,而只是返回false。 。

This line:

return ((sPtr[l] == sPtr[r]) && palindromeKernel(sPtr, l+1, r-1));

is the trick. C has what's called "short-circuit evaluation" of && and ||, so if the first leg of an && expression is false, it does not need to evaluate the second and does not, so when the two characters compared to not match (sPtr[l] == sPtr[r] is false), then it stops calling itself recursively and just returns false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文