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

时间:2016-11-30 17:13:22

标签: c arrays string pointers palindrome

我的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;
}

它总是打印:不是PALINDROME! 即使它是一个。

4 个答案:

答案 0 :(得分:1)

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

#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

答案 1 :(得分:1)

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

至于你的代码那么这些陈述

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):

答案 2 :(得分:0)

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

#include <string.h>

// ...

    right = strlen(sentence) - 1;

答案 3 :(得分:0)

你可以使用这种代码格式

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

int main()
{
    int length = 0;
    int i = 0;
    int count = 0;
    char data[100] = "ABCDEDCBA";
    length = strlen(data);
    printf("length is %d\n", length);

    while (i < length)
    {
        if (data[i] != data[length - i - 1])
        {
            count = 1;
        }
        i++;
    }
    if (count == 1)
    {
        printf(" not palindrome");
    }
    else
    {
        printf("palindrome");
    }

    return 0;
}
相关问题