编辑代码

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
//递归
bool isPalindromeRecursive(char str[], int start, int end) {
    if (start >= end)
        return true;
    else if (str[start] != str[end])
        return false;
    else
        return isPalindromeRecursive(str, start + 1, end - 1);
}
//递推
bool isPalindromeIterative(char str[],int start, int end) {
    while (start < end) {
        if (str[start] != str[end])
            return false;
        start++;
        end--;
    }
    return true;
}

int main() {
    char str1[] = "level"; 
    int length = strlen(str1);
    bool result1 = isPalindromeRecursive(str1, 0, length - 1);
    if (result1)
        printf("%s是回文字符串。\n", str1);
    else
        printf("%s不是回文字符串。\n", str1);
    bool result2 = isPalindromeIterative(str1, 0, length - 1);
    if (result2)
        printf("%s是回文字符串。\n", str1);
    else
        printf("%s不是回文字符串。\n", str1);
    

    char str2[] = "abcd"; 
    length = strlen(str2);
    result1 = isPalindromeRecursive(str2, 0, length - 1);
    if (result1)
        printf("%s是回文字符串。\n", str2);
    else
        printf("%s不是回文字符串。\n", str2);
    result2 = isPalindromeIterative(str2, 0, length - 1);
    if (result2)
        printf("%s是回文字符串。\n", str2);
    else
        printf("%s不是回文字符串。\n", str2);

    char str3[] = "abccba"; 
    length = strlen(str3);
    result1 = isPalindromeRecursive(str3, 0, length - 1);
    if (result1)
        printf("%s是回文字符串。\n", str3);
    else
        printf("%s不是回文字符串。\n", str3);
    result2 = isPalindromeIterative(str3, 0, length - 1);
    if (result2)
        printf("%s是回文字符串。\n", str3);
    else
        printf("%s不是回文字符串。\n", str3);
    return 0;
}