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

检查字符串是否为回文的C程序,c,arrays,string,pointers,palindrome,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(s

我的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;
}
#包括
#包括
int main()
{
char语句[39];
int左=0;
int右=40;
printf(“输入消息:”);
fgets(第40句,标准文本);
而(1){
while(左<右&!(isalpha(句子[左]))
左++;
while(right>left&!(isalpha(句子[right]))
对--;
如果(左>=右)
打破
否则{
如果(句子[左]!=句子[右]){
printf(“非回文”);
返回0;
}
左++;
对--;
}
}
printf(“回文”);
返回0;
}
它总是打印:不是回文!
即使是一个。

您也应该将右侧初始化为字符串的结尾:

#include <string.h>

// ...

    right = strlen(sentence) - 1;
#包括
// ...
右=斯特伦(句子)-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;
}
#包括
#包括
#包括
int main()
{
char句子[200];//提供足够的空间
int左=0;
int right;//不假定长度
printf(“输入消息:”);
fgets(句子,句子大小,标准输入);//限制输入
right=strlen(句子);//现在获取长度
而(1){
while(左<右&!(isalpha(句子[左]))
左++;
while(right>left&!(isalpha(句子[right]))
对--;
如果(左>=右)
打破
否则{
if(toupper(句子[左])!=toupper(句子[右]){//获得相同的大小写
printf(“不是回文\n”);
返回0;
}
左++;
对--;
}
}
printf(“回文\n”);
返回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个元素。此外,输入的字符串的kess可以超过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;
}

你试过基本的printf调试吗?您是否曾考虑过<代码> fgs<代码>在字符串结尾留下一个<代码> \n>代码>?<代码>句子(右)< /代码>在<代码>右= 40 这是初始值时无效。应该将字符转换为下位或大写。代码>H!=h。如果您有
字符句子[39]
中有39个字符。不是40岁。不是38岁<代码>fgets(第40句,标准文本)最多可填充40个字符。你只有39个。
句子中的最后一个索引是38。不是39,也不是40。是的,但在读取
语句时,它可能会更早崩溃,因为它只能容纳39个字节,但使用
fgets(语句,40,stdin)填充没错。他应该调整句子大小或更新FGET。
Enter a sentence (Enter - exit): He lived as a devil, eh

The sentence is palindrome.

Enter a sentence (Enter - exit):