繁体   English   中英

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

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

我的C程序有些挣扎! 它应该检查字符串是否是回文式! 它不应该注意非字母字符,因此程序应将其识别为回文! “他过着魔鬼的生活,是吗?” 那就是我到目前为止所得到的:

#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;
}

它总是在打印:不是回文! 即使是一个。

我对您的程序进行了一些更改。 首先不破坏数组索引,其次使用字符串长度而不是访问未定义的值,第三次检查相同的大小写字母。

#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;
}

计划会议:

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

Enter a message: palindrome
Not a Palindrome

您可以编写一个单独的函数来检查输入的句子是否是回文。

至于你的代码,那么这些语句

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

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

导致不确定的行为,因为在尝试输入40个字符时,数组语句只有39个元素。 同样,输入的字符串的字符数不能超过40个字符。 您需要确定字符串的长度。

这是一个演示程序,显示了如何编写相应的函数。

#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;
}

它的输出可能看起来像

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

The sentence is palindrome.

Enter a sentence (Enter - exit):

您应该将right初始化为字符串的结尾:

#include <string.h>

// ...

    right = strlen(sentence) - 1;

暂无
暂无

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

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