我一直在研究潛在的面試問題,其中之一就是在C中編寫一個函數來檢測給定的字符串是否是迴文。如何檢測C中的迴文?
我已經得到了它一個非常良好的開端:
#include <stdio.h>
#include <stdbool.h>
bool isPalindrome(char *value);
bool isPalindrome(char *value)
{
if (value == null)
return false;
char *begin = value;
char *end = begin + strlen(value) - 1;
while(*begin == *end)
{
if ((begin == end) || (begin+1 == end))
return true;
begin++;
end--;
}
return false;
}
int main()
{
printf("Enter a string: \n");
char text[25];
scanf("%s", text);
if (isPalindrome(text))
{
printf("That is a palindrome!\n");
}
else
{
printf("That is not a palindrome!\n");
}
}
不過,我現在想確保我忽略空格和標點符號。
鑑於上面所寫的代碼,如果他們遇到標點符號/空格,向前或向後推進指針,最好的方法是什麼?
這是一所學校的功課? – 2010-03-01 08:07:06
@Jojo,顯然你沒有理解這個問題。 – Waldrop 2010-03-01 08:29:35