用c语言写一个回文判断的算法
时间: 2024-05-22 17:15:25 浏览: 16
#include <stdio.h>
#include <string.h>
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; // 是回文
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s是回文\n", str);
} else {
printf("%s不是回文\n", str);
}
return 0;
}
相关问题
用C语言写一个算法实现求最长回文子串
最长回文子串是指一个字符串中最长的回文子串。回文字符串是指正着读和倒着读都一样的字符串。下面是用C语言实现求最长回文子串的算法:
1.暴力求解法:
暴力求解法的思路是枚举所有可能的子串,判断每个子串是否是回文串,并记录最长的回文串。具体实现过程如下:
```c
#include <stdio.h>
#include <string.h>
int isPalindrome(char s[], int start, int end) {
while (start < end) {
if (s[start] != s[end]) {
return 0;
}
start++;
end--;
}
return 1;
}
void longestPalindrome(char s[]) {
int len = strlen(s);
int maxLen = 0;
int maxStart = 0;
int i, j;
for (i = 0; i < len; i++) {
for (j = i; j < len; j++) {
if (isPalindrome(s, i, j)) {
int curLen = j - i + 1;
if (curLen > maxLen) {
maxLen = curLen;
maxStart = i;
}
}
}
}
printf("The longest palindrome substring is: ");
for (i = maxStart; i < maxStart + maxLen; i++) {
printf("%c", s[i]);
}
printf("\n");
}
int main() {
char s[100];
printf("Please input a string: ");
scanf("%s", s);
longestPalindrome(s);
return 0;
}
```
2.动态规划法:
动态规划法的思路是先求出所有长度较小的子串是否为回文串,然后再利用这些信息求出长度较大的子串是否为回文串,最终得到最长的回文子串。具体实现过程如下:
```c
#include <stdio.h>
#include <string.h>
void longestPalindrome(char s[]) {
int len = strlen(s);
int maxLen = 1;
int maxStart = 0;
int i, j;
int dp[len][len];
memset(dp, 0, sizeof(dp));
for (i = 0; i < len; i++) {
dp[i][i] = 1;
}
for (i = 0; i < len - 1; i++) {
if (s[i] == s[i + 1]) {
dp[i][i + 1] = 1;
maxLen = 2;
maxStart = i;
}
}
for (i = 2; i < len; i++) {
for (j = 0; j < len - i; j++) {
if (s[j] == s[j + i] && dp[j + 1][j + i - 1]) {
dp[j][j + i] = 1;
if (i + 1 > maxLen) {
maxLen = i + 1;
maxStart = j;
}
}
}
}
printf("The longest palindrome substring is: ");
for (i = maxStart; i < maxStart + maxLen; i++) {
printf("%c", s[i]);
}
printf("\n");
}
int main() {
char s[100];
printf("Please input a string: ");
scanf("%s", s);
longestPalindrome(s);
return 0;
}
```
以上就是用C语言实现求最长回文子串的算法。
c语言设计函数算法判断回文
以下是一个C语言设计函数算法判断回文的例子:
```c
#include <stdio.h>
#include <string.h>
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;
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s是回文字符串\n", str);
} else {
printf("%s不是回文字符串\n", str);
}
return 0;
}
```
该程序通过isPalindrome函数判断输入的字符串是否为回文字符串,如果是则输出“是回文字符串”,否则输出“不是回文字符串”。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)