c语言递归法判断回文字符串
时间: 2024-10-25 10:10:34 浏览: 55
在 C 语言中,递归法判断回文字符串通常涉及到比较字符串正向和反向是否相等。回文字符串是指从前往后读和从后往前读都一样的字符串。下面是一个简单的递归函数示例:
```c
#include <stdio.h>
#include <string.h>
// 递归函数,用于检查字符串是否回文
int isPalindrome(char str[], int start, int end) {
// 如果字符串长度为0或1,它是回文的
if (start >= end)
return 1;
// 比较第一个字符和最后一个字符
if (str[start] != str[end])
return 0;
// 递归地检查剩余部分(去掉首尾字符)
return isPalindrome(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("请输入一个字符串: ");
fgets(str, sizeof(str), stdin); // 防止换行符
str[strlen(str) - 1] = '\0'; // 去掉fgets添加的换行符
// 判断并打印结果
if (isPalindrome(str, 0, strlen(str) - 1))
printf("%s 是回文\n", str);
else
printf("%s 不是回文\n", str);
return 0;
}
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)