2010-03-01 56 views
5

我一直在研究潛在的面試問題,其中之一就是在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"); 
    } 
} 

不過,我現在想確保我忽略空格和標點符號。

鑑於上面所寫的代碼,如果他們遇到標點符號/空格,向前或向後推進指針,最好的方法是什麼?

+0

這是一所學校的功課? – 2010-03-01 08:07:06

+3

@Jojo,顯然你沒有理解這個問題。 – Waldrop 2010-03-01 08:29:35

回答

5

變化環路

while(begin < end) { 
    while(ispunct(*begin) || isspace(*begin)) 
    ++begin; 
    while(ispunct(*end) || isspace(*end)) 
    --end; 
    if(*begin != *end) 
    return false; 
    ++begin; 
    --end; 
} 
return true; 
+3

+1,而不是'ispunct(x)|| isspace(x)'我可能會使用'!isalpha(x)'。這有點不同,但在我看來,這在眼睛上更容易一些。 – 2010-03-01 05:12:18

+3

這將完全由標點符號組成的字符串(等等)失敗。您需要在這些循環中進行更多檢查,以確保'end'和'begin'在跳過標點符號時不會相互傳遞。 – caf 2010-03-01 05:37:09

+0

@caf,'ispunct(0)'是錯誤的,所以'begin'將會很好 - 你需要在'if'中加一個'&&(end> value)'來監視'--end',雖然。 – 2010-03-01 06:40:35

0

如何寫另一個函數刪除字符串的空間和標點字符?

+2

嗯,我可以做到這一點。我想這似乎更有意義,如果我通過推進指針來消耗它們。 – Waldrop 2010-03-01 05:07:49

+0

同意;如果不需要,最好不要爲新複製的字符串分配空間。 – 2010-03-01 05:47:04

3

在while循環,只是跳過要忽略任何字符:

while(*begin == *end) 
{ 
    while ((begin != end) && (isspace(*begin) || isX(*begin)) 
     ++begin; 

    // and something similar for end 

另外一個評論。由於你的功能不修改參數,你應該把它定義爲:

bool isPalindrome(const char *value); 
+0

+1:用於在簽名中添加「const」。同樣的原則,「const char * begin」和「const char * end」! – Arun 2010-03-01 07:02:33

0

請參考下面的例子中,檢查字符串是否是迴文

 

main() 
{ 
     char str[100] ; 
     printf ("enter string:"); 
     scanf ("%s" ,str) ; 
     if (ispalindorm(str)) 
     { 
       printf ("%s is palindrome \n"); 
     } 
     else 
     { 
       printf ("%s is not a palindrome \n") ; 
     } 
} 
int ispalindorm ( char str[]) 
{ 
     int i , j ; 
     for (i=0,j=strlen(str)-1;i < strlen(str)-1&& (j>0) ;i++,j--) 
     { 
       if (str[i] != str[j]) 
         return 0 ; 
     } 
     return 1 ; 
} 
+0

@pavun_cool,你沒有看到這個問題。該解決方案根本不處理空格或標點符號。 – Waldrop 2010-03-01 08:34:36

0

這是我對其採取,試圖做到簡潔。此外,剛剛添加檢查是否有輸入

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

int p_drome(char *c) { 
    int beg=0, end = strlen(c)-1; 
    for (;c[beg]==c[end] && beg<strlen(c)/2;beg++,end--); 
    return (beg == strlen(c)/2) ? 1 : 0; 
} 

int main(int argc, char* argv[]) { 
    argv[1]?(p_drome(argv[1])?printf("yes\n"):printf("no\n")):printf("no input\n"); 
} 
0
/* you can use this code to check the palindrome*/  
#include<stdio.h> 
    #include<string.h> 
    int is_pali(char str1[]); 
    int is_pali(char str1[]) 
    { 
     char str2[100]; 
     int n,i; 
     n = strlen(str1); 
     for(i=0;i<n;i++) 
     str2[n-1-i] = str1[i]; 
     if(str1[i]=str2[i]) 
     return 0; 
     else 
     return 1; 
    } 
    int main() 
    { 
     char str1[100]; 
     int temp; 
     printf("Enter the string\n"); 
     gets(str1); 
     temp = is_pali(str1); 
     if (temp==0) 
     printf("the given string is not palindrome\n"); 
     else 
     printf("the given string is palindrome\n"); 
    } 
-1
#include<stdio.h> 
    #include<string.h> 
int main() 
{ 
char str[20]; 

int i,j,k,m,n; 
printf("enter the string\n"); 
scanf("%s",str); 
printf("%s",str); 
k=strlen(str); 
printf("\nthe lenght of string is %d",k); 

for(i=0;i<k/2;i++) 
{ 
    m=str[i]; 
    n=str[k-1-i]; 
}if(m==n) 
{ 

printf("\nthe given string is palindrome");   
} 
else{ 
printf("\nthe given string is not a palindrome"); 
     } 
return 0; 

}