2010-03-01 51 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; 

}