这是我在 c 中制作的回文检查器。它适用于所有输入,无论它们是否有标点符号,除非最后一项是标点符号。在这种情况下,它不会跳过它并进行比较,然后说它不是回文,而实际上它是。EX(活过,魔鬼。不会是回文,而是活过,魔鬼会)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#define max 180
bool is_palindrome(const char *message);
int main()
{
char message[max+1];
printf("Enter a message: ");
gets(message);
if(!*message)
{
printf("input error");
return 0;
}
if (is_palindrome(message)) printf("Palindrome\n");
else printf("Not a palindrome");
return 0;
}
bool is_palindrome(const char *message)
{
char *p, *p2;
bool palindrome = true;
p = message;
p2 = message;
for(;;)
{
while(*p)p++;
while(*p2)
{
while(!isalpha(*p)) p--;
while(!isalpha(*p2)) p2++;
if (toupper(*p) != toupper(*p2))
{
palindrome = false;
break;
}else
{
p--;
p2++;
}
}
break;
}
return palindrome;
}