编写函数,判断一个字符串是否是回文。在主函数中输入一个字符串,调用自定义函数,输出结果。所谓回文是指顺读和倒读都一样的字符串。如“XZYKYZX”是回文。用c语言
时间: 2024-02-16 12:04:47 浏览: 94
好的,以下是用 C 语言编写的判断回文字符串的代码:
```c
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]);
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s 是回文字符串。\n", str);
} else {
printf("%s 不是回文字符串。\n", str);
}
return 0;
}
int isPalindrome(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
return 0;
}
}
return 1;
}
```
运行程序后,输入一个字符串,程序会调用 `isPalindrome` 函数来判断该字符串是否是回文字符串,并输出结果。
相关问题
编写函数,判断一个字符串是否是回文。主函数输入字符串,调用自定义函数,输出结果。
好的,以下是实现该功能的 Python 代码:
```python
def is_palindrome(s):
# 将字符串反转
reversed_s = s[::-1]
# 判断反转后的字符串是否与原字符串相等
if s == reversed_s:
return True
else:
return False
# 主函数
if __name__ == '__main__':
s = input("请输入一个字符串:")
if is_palindrome(s):
print("是回文字符串")
else:
print("不是回文字符串")
```
该函数接受一个字符串作为参数,将其反转后再与原字符串比较,如果相等,则说明原字符串是回文字符串。在主函数中,我们通过输入字符串调用自定义函数,并根据返回值输出结果。
编写函数,判断一个字符串是否是回文。在主函数中输入一个字符串,调用自定义函数,输出结果
好的,以下是代码实现:
```python
def is_palindrome(s):
"""
判断字符串是否是回文
"""
return s == s[::-1]
if __name__ == '__main__':
s = input("请输入一个字符串:")
if is_palindrome(s):
print("是回文")
else:
print("不是回文")
```
这里我们定义了一个 `is_palindrome` 函数,用来判断一个字符串是否是回文。然后在主函数中,我们通过 `input` 函数获取用户输入的字符串,然后调用自定义函数 `is_palindrome` 进行判断,最后输出结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)