如何用C语言判断回文

如何用C语言判断回文

如何用C语言判断回文

在C语言中判断一个字符串是否为回文的核心步骤包括:首先,对字符串进行预处理;接着,通过双指针或栈的方法进行比较;最后,得出结论。预处理字符串、双指针比较、栈方法、边界条件处理。下面将详细介绍如何用C语言实现这些步骤,并给出完整的代码示例。

预处理字符串

在实际编程过程中,字符串的预处理非常重要。主要的预处理步骤包括去除非字母字符、统一大小写等。这样做的目的是为了避免不必要的复杂性,使得判断过程更加直接和可靠。

一、预处理字符串

在处理回文问题时,首先需要处理字符串中的各种字符。这包括去除空格、标点等非字母字符,并将所有字符转换为小写。下面是一个简单的预处理函数:

#include <ctype.h>

#include <string.h>

// 预处理函数,将字符串中的非字母字符去除,并将所有字符转换为小写

void preprocess(char* str) {

int length = strlen(str);

int index = 0;

for (int i = 0; i < length; i++) {

if (isalpha(str[i])) {

str[index++] = tolower(str[i]);

}

}

str[index] = ''; // 结束字符串

}

二、双指针方法

双指针方法是一种高效且简单的回文判断方法。具体做法是设置两个指针,一个从字符串的起始位置开始,另一个从字符串的末尾位置开始,依次向中间移动,比较所指向的字符是否相等。如果始终相等,则字符串为回文;如果有任何不相等的情况,则字符串不是回文。

#include <stdbool.h>

// 双指针方法判断回文

bool isPalindrome(char* str) {

int left = 0;

int right = strlen(str) - 1;

while (left < right) {

if (str[left] != str[right]) {

return false;

}

left++;

right--;

}

return true;

}

三、栈方法

栈方法是另一种判断回文的经典方法。通过将字符串的前半部分压入栈中,然后依次弹出与字符串的后半部分进行比较,可以判断字符串是否为回文。

#include <stdio.h>

#include <stdlib.h>

// 栈结构定义

typedef struct {

char* data;

int top;

int capacity;

} Stack;

// 创建栈

Stack* createStack(int capacity) {

Stack* stack = (Stack*)malloc(sizeof(Stack));

stack->data = (char*)malloc(capacity * sizeof(char));

stack->top = -1;

stack->capacity = capacity;

return stack;

}

// 压栈

void push(Stack* stack, char ch) {

if (stack->top < stack->capacity - 1) {

stack->data[++stack->top] = ch;

}

}

// 弹栈

char pop(Stack* stack) {

if (stack->top >= 0) {

return stack->data[stack->top--];

}

return ''; // 栈空返回空字符

}

// 判断回文(栈方法)

bool isPalindromeStack(char* str) {

int length = strlen(str);

Stack* stack = createStack(length / 2);

for (int i = 0; i < length / 2; i++) {

push(stack, str[i]);

}

for (int i = (length + 1) / 2; i < length; i++) {

if (str[i] != pop(stack)) {

free(stack->data);

free(stack);

return false;

}

}

free(stack->data);

free(stack);

return true;

}

四、完整代码示例

将上述各个部分整合在一起,给出完整的C语言判断回文的代码示例:

#include <stdio.h>

#include <ctype.h>

#include <string.h>

#include <stdbool.h>

#include <stdlib.h>

void preprocess(char* str) {

int length = strlen(str);

int index = 0;

for (int i = 0; i < length; i++) {

if (isalpha(str[i])) {

str[index++] = tolower(str[i]);

}

}

str[index] = '';

}

bool isPalindrome(char* str) {

int left = 0;

int right = strlen(str) - 1;

while (left < right) {

if (str[left] != str[right]) {

return false;

}

left++;

right--;

}

return true;

}

typedef struct {

char* data;

int top;

int capacity;

} Stack;

Stack* createStack(int capacity) {

Stack* stack = (Stack*)malloc(sizeof(Stack));

stack->data = (char*)malloc(capacity * sizeof(char));

stack->top = -1;

stack->capacity = capacity;

return stack;

}

void push(Stack* stack, char ch) {

if (stack->top < stack->capacity - 1) {

stack->data[++stack->top] = ch;

}

}

char pop(Stack* stack) {

if (stack->top >= 0) {

return stack->data[stack->top--];

}

return '';

}

bool isPalindromeStack(char* str) {

int length = strlen(str);

Stack* stack = createStack(length / 2);

for (int i = 0; i < length / 2; i++) {

push(stack, str[i]);

}

for (int i = (length + 1) / 2; i < length; i++) {

if (str[i] != pop(stack)) {

free(stack->data);

free(stack);

return false;

}

}

free(stack->data);

free(stack);

return true;

}

int main() {

char str[100];

printf("请输入一个字符串: ");

fgets(str, sizeof(str), stdin);

str[strcspn(str, "n")] = ''; // 移除换行符

preprocess(str);

if (isPalindrome(str)) {

printf("该字符串是回文.n");

} else {

printf("该字符串不是回文.n");

}

if (isPalindromeStack(str)) {

printf("(使用栈方法)该字符串是回文.n");

} else {

printf("(使用栈方法)该字符串不是回文.n");

}

return 0;

}

通过上述代码,我们能够实现对字符串的回文判断。不论是采用双指针方法还是栈方法,都需要进行字符串的预处理。双指针方法较为简洁,而栈方法则在某些情况下更具直观性。根据实际需要选择合适的方法,可以更高效地解决问题。

相关问答FAQs:

1. 什么是回文?

回文是指正序和逆序读都一样的字符串或数字。例如,"level"和"1221"都是回文。

2. 如何用C语言判断一个字符串是否是回文?

要判断一个字符串是否是回文,可以使用双指针法。定义两个指针分别指向字符串的首尾,然后逐步向中间移动指针并比较对应位置的字符是否相同,如果有不相同的字符,则该字符串不是回文。如果最终指针相遇,则说明该字符串是回文。

下面是一个示例代码:

#include <stdio.h>
#include <string.h>

int isPalindrome(char *str) {
    int start = 0;
    int end = strlen(str) - 1;
    
    while (start < end) {
        if (str[start] != str[end]) {
            return 0; // 不是回文
        }
        start++;
        end--;
    }
    
    return 1; // 是回文
}

int main() {
    char str[100];
    
    printf("请输入一个字符串:");
    scanf("%s", str);
    
    if (isPalindrome(str)) {
        printf("该字符串是回文。n");
    } else {
        printf("该字符串不是回文。n");
    }
    
    return 0;
}

3. 如何用C语言判断一个数字是否是回文?

要判断一个数字是否是回文,可以将其转换为字符串,然后使用上述的字符串回文判断方法。首先,将数字转换为字符串,可以使用sprintf函数。然后,将得到的字符串作为参数调用isPalindrome函数进行回文判断。如果返回1,则说明该数字是回文。

下面是一个示例代码:

#include <stdio.h>
#include <string.h>

int isPalindrome(char *str) {
    int start = 0;
    int end = strlen(str) - 1;
    
    while (start < end) {
        if (str[start] != str[end]) {
            return 0; // 不是回文
        }
        start++;
        end--;
    }
    
    return 1; // 是回文
}

int main() {
    int num;
    char str[100];
    
    printf("请输入一个数字:");
    scanf("%d", &num);
    
    sprintf(str, "%d", num); // 将数字转换为字符串
    
    if (isPalindrome(str)) {
        printf("该数字是回文。n");
    } else {
        printf("该数字不是回文。n");
    }
    
    return 0;
}

文章包含AI辅助创作,作者:Edit1,如若转载,请注明出处:https://docs.pingcode.com/baike/980527

(0)
Edit1Edit1
免费注册
电话联系

4008001024

微信咨询
微信咨询
返回顶部