C语言编写程序判断给定字符串是否是回文。(回文是指正读和逆读都一样的字符串。)
时间: 2023-12-15 15:07:03 浏览: 96
可以使用双指针法,从字符串两端开始逐一比较字符是否相等,直到中间位置。具体实现代码如下:
#include <stdio.h>
#include <string.h>
int isPalindrome(char* str) {
int len = strlen(str);
int left = 0, right = len - 1;
while (left < right) {
if (str[left] != str[right]) {
return 0;
}
left++;
right--;
}
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;
}
当输入字符串为“abcba”时,输出为“abcba是回文字符串。”。
相关问题
编写程序判断给定字符串是否是回文。(回文是指正读和逆读都一样的字符串。) 如: 输入:12321, 输出:是回文。 输入:12345, 输出: 不是回文。用c语言
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, j, len, flag = 1;
printf("请输入字符串:");
scanf("%s", str);
len = strlen(str);
for (i = 0, j = len-1; i < len/2; i++, j--) {
if (str[i] != str[j]) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("是回文。\n");
} else {
printf("不是回文。\n");
}
return 0;
}
请用c语言编写一个完整的程序:回文是指正读反读均相同的字符序列,如ABBA,abdba均是回文,good不是回文。利用栈结构设计算法判定给定的字符串是否为回文。
在C语言中,我们可以使用栈数据结构来检查一个字符串是否是回文。以下是一个简单的C程序示例:
```c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
// 定义栈结构体
typedef struct {
char *data;
int top;
int size;
} Stack;
// 初始化栈
void init(Stack *s) {
s->top = -1;
s->size = 5; // 初始化大小为5, 可根据实际情况调整
s->data = (char*)malloc(s->size * sizeof(char));
}
// 判断栈是否为空
bool is_empty(Stack *s) {
return s->top == -1;
}
// 入栈操作
void push(Stack *s, char c) {
if (is_full(s)) {
printf("Stack overflow.\n");
return;
}
s->data[++(s->top)] = c;
}
// 出栈操作
char pop(Stack *s) {
if (is_empty(s)) {
printf("Stack underflow.\n");
return '\0';
}
return s->data[s->top--];
}
// 检查栈是否已满
bool is_full(Stack *s) {
return s->top == s->size - 1;
}
// 主函数
bool is_palindrome(char* str) {
Stack s;
init(&s);
// 将字符串反转并入栈
for(int i = strlen(str) - 1; i >= 0; i--) {
push(&s, str[i]);
}
// 从栈顶开始对比原串和翻转后的串
for(int j = 0; j < strlen(str); j++) {
if(pop(&s) != str[j]) {
return false;
}
}
return true;
}
int main() {
char input[100];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // 移除fgets读取的换行符
if(is_palindrome(input)) {
printf("%s is a palindrome.\n", input);
} else {
printf("%s is not a palindrome.\n", input);
}
free(s.data); // 注意释放内存
return 0;
}
```
这个程序首先定义了一个栈结构,然后初始化、入栈、出栈以及判断栈的状态。`is_palindrome`函数通过将字符串逆序后逐个与原字符串进行比较,如果所有元素都匹配则返回true,表示该字符串是回文。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)