#include <stdio.h>#include <string.h>int checkPalindrome(char *str, int start, int end) { if (start >= end) { return 1; } if (str[start] != str[end]) { return 0; } return checkPalindrome(str, start + 1, end - 1);}int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); if (checkPalindrome(str, 0, strlen(str) - 1)) { printf("The string is a palindrome permutation.\n"); } else { printf("The string is not a palindrome permutation.\n"); } return 0;}
这段代码首先定义了一个名为checkPalindrome
的函数,该函数用于检测给定字符串是否为回文排列。函数的递归思想是,从字符串的开头和结尾开始比较字符是否相等,逐步向中间靠拢,直到整个字符串被检测完毕。如果在任何时候发现不相等的字符,则返回0,否则返回1。
在main
函数中,用户输入一个字符串,然后调用checkPalindrome
函数进行检测。根据函数的返回值,输出相应的结果。
可以通过在终端中编译并运行该程序,输入一个字符串,程序将告诉你该字符串是否为回文排列。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1007802.html