c语言设计函数算法判断回文
时间: 2023-11-28 12:45:02 浏览: 35
以下是一个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语言函数指针判断回文
下面是使用C语言函数指针判断回文的代码示例:
```c
#include <stdio.h>
#include <string.h>
int isPalindrome(char *str, int (*cmp)(const char *, const char *));
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str, strcmp)) {
printf("%s 是回文字符串\n", str);
} else {
printf("%s 不是回文字符串\n", str);
}
return 0;
}
int isPalindrome(char *str, int (*cmp)(const char *, const char *)) {
int len = strlen(str);
char *p1 = str;
char *p2 = str + len - 1;
while (p1 < p2) {
if (cmp(p1, p2) != 0) {
return 0;
}
p1++;
p2--;
}
return 1;
}
```
该程序中,isPalindrome函数接收两个参数,第一个参数是要判断的字符串,第二个参数是一个函数指针,用于比较两个字符是否相等。在主函数中,我们使用strcmp函数作为第二个参数传递给isPalindrome函数。
isPalindrome函数首先统计字符串的长度,然后定义两个指针p1和p2,分别指向字符串的首尾字符。在比较过程中,如果发现p1和p2指向的字符不相等,就返回0,表示该字符串不是回文字符串。如果一直比较到p1和p2相遇,就说明该字符串是回文字符串,返回1。
只用C语言顺序栈实现判断回文
使用C语言顺序栈可以实现判断回文的功能。回文是指正读和反读都相同的字符串。下面是使用顺序栈实现判断回文的步骤:
1. 定义一个顺序栈结构,包括栈的容量、栈顶指针和存储元素的数组。
2. 初始化栈,将栈顶指针置为-1。
3. 将待判断的字符串依次入栈,直到字符串结束。
4. 从字符串的开头开始遍历,将每个字符出栈并与当前遍历的字符进行比较。
- 如果相等,则继续遍历下一个字符。
- 如果不相等,则说明不是回文,返回false。
5. 如果遍历完整个字符串,且栈为空,则说明是回文,返回true;否则,返回false。
下面是使用C语言实现判断回文的代码示例:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void initStack(Stack* stack) {
stack->top = -1;
}
bool isEmpty(Stack* stack) {
return stack->top == -1;
}
bool isFull(Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
void push(Stack* stack, char c) {
if (isFull(stack)) {
printf("Stack is full.\n");
return;
}
stack->data[++stack->top] = c;
}
char pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return '\0';
}
return->data[stack->top--];
}
bool isPalindrome(char* str) {
Stack stack;
initStack(&stack);
// 将字符串入栈
int i = 0;
while (str[i] != '\0') {
push(&stack, str[i]);
i++;
}
// 判断是否为回文
i = 0;
while (str[i] != '\0') {
if (pop(&stack) != str[i]) {
return false;
}
i++;
}
return isEmpty(&stack);
}
int main() {
char str[MAX_SIZE];
printf("请输入一个字符串:");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s 是回文。\n", str);
} else {
printf("%s 不是回文。\n", str);
}
return 0;
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)