如何检测C中的回文?

时间:2010-03-01 04:59:43

标签: 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");
    }
}

但是,我现在想确保忽略空格和标点符号。

根据我上面编写的代码,如果遇到标点符号/空格,向前或向后推进指针的最佳方法是什么?

7 个答案:

答案 0 :(得分: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;

答案 1 :(得分:3)

在while循环中,只需跳过要忽略的任何字符:

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

   // and something similar for end

另一条评论。由于您的函数未修改参数,因此您应将其定义为:

bool isPalindrome(const char *value);

答案 2 :(得分:0)

如何编写另一个函数来删除字符串中的空格和标点字符?

答案 3 :(得分: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 ;
}

答案 4 :(得分: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");
}

答案 5 :(得分: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");
    }

答案 6 :(得分:-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;

}