简体   繁体   English

递归函数查找回文

[英]Recursive function finding Palindrome

May be you can tell me the way, that I can start with, at least.也许你可以告诉我方法,至少我可以开始。 I can use only C language.我只能使用C语言。 The task have very specific limitations and I can't break them in any way.该任务有非常具体的限制,我不能以任何方式打破它们。 The task is:任务是:

  • Write recursive function that checks if string is Palindrome.编写递归函数来检查字符串是否为回文。
  • Can use strlen() only once in function.只能在函数中使用strlen()一次。
  • Can't use any loops or functions based on loops.不能使用任何循环或基于循环的函数。
  • Can use only one transition in recursive function.在递归函数中只能使用一种转换。
  • Can change the string, but only if it will come back in the end of function.可以更改字符串,但前提是它会在函数结束时返回。
  • Declaration of function is: int palindrom(char* str);函数声明为: int palindrom(char* str);
  • I started to write, but have no ideas anymore:我开始写,但没有想法了:

     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int palindrom(char* str) { int len = strlen(str); if (str[0] != str[len - 1]) return 0; } int main(void) { char string1[] = "ROTATOR"; char string2[] = "8536358"; char string3[] = "Palindrome"; if (palindrom(string1)) printf("%s is Palindrome\\n", string1); else printf("%s is not Palindrome\\n", string1); if (palindrom(string2)) printf("%s is Palindrome\\n", string2); else printf("%s is not Palindrome\\n", string2); if (palindrom(string3)) printf("%s is Palindrome\\n", string3); else printf("%s is not Palindrome\\n", string3); return 0; }

Something like this will work ?这样的事情会起作用吗?

Something like this will work ?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

bool palindrom_helper(char* str, int first, int last)
{
    if(first >= last)
        return true;
    if (str[first] != str[last]) 
        return false;
    return (palindrom(str, first+1, last-1));

}

bool palindrom(char* str)
{
    return palindrom_helper(str, 0, strlen(str)-1);
}

int main(void)
{
    char string1[] = "ROTATOR";
    char string2[] = "8536358";
    char string3[] = "Palindrome";
    if (palindrom(string1)) printf("%s is Palindrome\n", string1);
    else printf("%s is not Palindrome\n", string1);
    if (palindrom(string2)) printf("%s is Palindrome\n", string2);
    else printf("%s is not Palindrome\n", string2);
    if (palindrom(string3)) printf("%s is Palindrome\n", string3);
    else printf("%s is not Palindrome\n", string3);
    return 0;
}

you should use the strlen function only once.您应该只使用一次 strlen 函数。 so you cannot use it inside the function that's being called recursively.所以你不能在递归调用的函数中使用它。

what i did here is to initialize first and last to 0 and len-1 and then recurse with (first+1, last-1).我在这里所做的是将 first 和 last 初始化为 0 和 len-1,然后使用 (first+1, last-1) 进行递归。

if the function finds even one pair of unmatching letters, it'd return false.如果该函数甚至找到一对不匹配的字母,它就会返回 false。 else it'd continue until they reach the center together (odd length string) or cross each other (even length string) and then return true (because that'd mean they did not see any unmatching letter on their path)否则它会继续直到它们一起到达中心(奇数长度的字符串)或彼此交叉(偶数长度的字符串),然后返回 true(因为这意味着它们在路径上没有看到任何不匹配的字母)

Also, i dont understand what you mean by single transition in the recursive function ?另外,我不明白递归函数中的单个转换是什么意思?

The clue to how to solve is in the restriction: Can change the string, but only if it will come back in the end of function.如何解决的线索在限制中:可以更改字符串,但前提是它会在函数结束时返回。 That condition is checked by printing the result.通过打印结果来检查该条件。

#include <stdio.h>
#include <string.h>

int palindrom(char* str)
{
    size_t len = strlen(str);
    int res;
    if(len < 2) {
        return 1;                   // cannot shorten: must be success
    }
    if(str[0] != str[len - 1]) {    // make palindrome test
        return 0;
    }

    str[len - 1] = '\0';            // shorten the string at the back
    res = palindrom(str + 1);       // recurse woth string shortened at the front
    str[len - 1] = str[0];          // replace last char (we know it's the same)
    return res;
}

int main(void)
{
    char string1[] = "ROTATOR";
    char string2[] = "8536358";
    char string3[] = "Palindrome";
    char string4[] = "A";
    char *wrd[] = { "not ", "" };

    printf("%s is %sa Palindrome\n", string1, wrd[ palindrom(string1) ]);
    printf("%s is %sa Palindrome\n", string2, wrd[ palindrom(string2) ]);
    printf("%s is %sa Palindrome\n", string3, wrd[ palindrom(string3) ]);
    printf("%s is %sa Palindrome\n", string4, wrd[ palindrom(string4) ]);

    return 0;
}

Program output:程序输出:

ROTATOR is a Palindrome
8536358 is a Palindrome
Palindrome is not a Palindrome
A is a Palindrome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM