简体   繁体   English

C程序检查字符串是否为回文

[英]C Program to check if string is Palindrome or not

i have some struggles with my C program! 我的C程序有些挣扎! It should check wether a string is a palindrome or not! 它应该检查字符串是否是回文式! it should not pay attention to non-alphabetic characters, so the program should recognize this as a palindrome! 它不应该注意非字母字符,因此程序应将其识别为回文! "He lived as a devil, eh?" “他过着魔鬼的生活,是吗?” That's what i got so far: 那就是我到目前为止所得到的:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char sentence[39];
    int left = 0;
    int right = 40;

    printf("Enter a message: ");
    fgets(sentence, 40, stdin);

    while(1) {

        while(left < right && !(isalpha(sentence[left])))
            left++;
        while(right > left && !(isalpha(sentence[right])))
            right--;

        if(left >= right)
            break;

        else {

            if(sentence[left] != sentence[right]) {
                printf("Not a Palindrome");
                return 0;
            }

            left++;
            right--;
        }
    }

    printf("Palindrome");

    return 0;
}

It's always printing: NOT A PALINDROME! 它总是在打印:不是回文! Even if it is one. 即使是一个。

I have made a few changes to your program. 我对您的程序进行了一些更改。 Firstly not breaking the array indexing, next using the string length instead of accessing undefined values, third checking same case letters. 首先不破坏数组索引,其次使用字符串长度而不是访问未定义的值,第三次检查相同的大小写字母。

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

int main()
{
    char sentence[200];                             // provide plenty of room
    int left = 0;
    int right;                                      // do not assume the length

    printf("Enter a message: ");
    fgets(sentence, sizeof sentence, stdin);        // limit the input
    right = strlen(sentence);                       // now get the length

    while(1) {
        while(left < right && !(isalpha(sentence[left])))
            left++;
        while(right > left && !(isalpha(sentence[right])))
            right--;
        if(left >= right)
            break;
        else {
            if(toupper(sentence[left]) != toupper(sentence[right])) {  // get case the same
                printf("Not a Palindrome\n");
                return 0;
            }
            left++;
            right--;
        }
    }

    printf("Palindrome\n");
    return 0;
}

Program sessions: 计划会议:

Enter a message: He lived as a devil, eh?
Palindrome

Enter a message: palindrome
Not a Palindrome

You could write a separate function that checks whether the inputted sentence is a palindrome. 您可以编写一个单独的函数来检查输入的句子是否是回文。

As for your code then these statements 至于你的代码,那么这些语句

char sentence[39];
int left = 0;
int right = 40;

printf("Enter a message: ");
fgets(sentence, 40, stdin);

lead to undefined behavior because the array sentence has only 39 elements while you are trying to enter 40 characters. 导致不确定的行为,因为在尝试输入40个字符时,数组语句只有39个元素。 Also the entered string can have kess than 40 characters. 同样,输入的字符串的字符数不能超过40个字符。 You need to determine the length of the string. 您需要确定字符串的长度。

Here is a demonstrative program that shows how a corresponding function can be written. 这是一个演示程序,显示了如何编写相应的函数。

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

int is_palindrome(const char *s)
{
    size_t n = strlen(s);

    const char *first = s, *last = s + n;

    if (n)
    {

        do
        {
            while ( *first && !isalpha((unsigned char)*first)) ++first;
            if (first != last)
            {
                while (!isalpha((unsigned char)*--last));
            }
        } while ( toupper( ( unsigned char )*first ) == 
                  toupper( ( unsigned char )*last ) && 
                  first != last && 
                  ++first != last);
    }

    return first == last;
}

#define N   100

int main()
{
    while (1)
    {
        char s[N];

        printf("Enter a sentence (Enter - exit): ");

        if (!fgets(s, sizeof(s), stdin) || s[0] == '\n') break;

        printf("\nThe sentence is%s palindrome.\n\n",
            is_palindrome(s) ? "" : " not");
    }

    return 0;
}

Its output might look like 它的输出可能看起来像

Enter a sentence (Enter - exit): He lived as a devil, eh

The sentence is palindrome.

Enter a sentence (Enter - exit):

You should initialize the right as the end of the string: 您应该将right初始化为字符串的结尾:

#include <string.h>

// ...

    right = strlen(sentence) - 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM