如何检测C中的回文?

如何检测C中的回文?,c,C,我一直在研究潜在的面试问题,其中一个问题是用C语言编写一个函数来检测给定的字符串是否是回文 我已经有了一个很好的开始: #include <stdio.h> #include <stdbool.h> bool isPalindrome(char *value); bool isPalindrome(char *value) { if (value == null) return false; char *begin = value;

我一直在研究潜在的面试问题,其中一个问题是用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");
    }
}
#包括
#包括
bool isPalindrome(字符*值);
bool isPalindrome(字符*值)
{
如果(值==null)
返回false;
char*begin=value;
char*end=begin+strlen(值)-1;
而(*开始==*结束)
{
如果((开始==结束)| |(开始+1==结束))
返回true;
begin++;
结束--;
}
返回false;
}
int main()
{
printf(“输入字符串:\n”);
字符文本[25];
scanf(“%s”,文本);
如果(isPalindrome(文本))
{
printf(“这是回文!\n”);
}
其他的
{
printf(“这不是回文!\n”);
}
}
但是,我现在想确保忽略空格和标点符号

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

将循环更改为

while(begin < end) {
  while(ispunct(*begin) || isspace(*begin))
    ++begin;
  while(ispunct(*end) || isspace(*end))
    --end;
  if(*begin != *end)
    return false;
  ++begin;
  --end;
}
return true;
while(开始<结束){
while(ispunt(*begin)| isspace(*begin))
++开始;
while(ispunt(*end)| | isspace(*end))
--结束;
如果(*开始!=*结束)
返回false;
++开始;
--结束;
}
返回true;

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

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

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

   // and something similar for end
还有一条评论。由于函数未修改参数,因此应将其定义为:

bool isPalindrome(const char *value);
请参考以下示例检查字符串是否为回文

main() { char-str[100]; printf(“输入字符串:”); scanf(“%s”,str); if(ispalindorm(str)) { printf(“%s”是回文\n”); } 其他的 { printf(“%s不是回文\n”); } } int ispalindorm(字符str[]) { int i,j; 对于(i=0,j=strlen(str)-1;i0);i++,j--) { 如果(str[i]!=str[j]) 返回0; } 返回1; }
这是我的看法,尽量简明扼要。另外,刚刚添加了检查无输入

#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");
}
#包括
#包括
国际机场(char*c){
int-beg=0,end=strlen(c)-1;
对于(;c[beg]==c[end]&&beg
/*您可以使用此代码检查回文*/
#包括
#包括
int是_-pali(char str1[]);
int是_-pali(char str1[])
{
char-str2[100];
int n,i;
n=strlen(str1);
对于(i=0;i
#包括
#包括
int main()
{
char-str[20];
int i,j,k,m,n;
printf(“输入字符串\n”);
scanf(“%s”,str);
printf(“%s”,str);
k=strlen(str);
printf(“\n字符串长度为%d”,k);

对于(i=0;iWell,我可以这么做。我想如果我通过推进指针来消耗它们似乎更有意义。同意;如果不需要的话,最好不要为新复制的字符串分配空间。+1而不是
ispunt(x)| | isspace(x)
,我可能会使用
!isalpha(x)
。虽然略有不同,但在我看来这更容易看。对于完全由标点符号(以及其他)组成的字符串,这将失败。您需要在这些循环中进行更多检查,以确保
end
begin
在跳过标点符号时不会互相传递。@caf,
ispunt(0)
为false,因此
begin
就可以了——您确实需要添加一个
&(end>值)
中,如果
保护
--end
,尽管如此。@chris,
!isalpha
也会跳过数字,这不是OP的规范所说的--只会跳过空格和标点符号。@Alex-这是真的,我把我的函数搞混了。我的意思是
isalnum
,就像@jbcreix所说的。+1:用于添加“const”在签名中。根据相同的原则,“const char*begin”和“const char*end”!@Jojo,显然你没有读这个问题。@pavun_酷,你没有读这个问题。这个解决方案根本不处理空格或标点符号。
#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");
}
/* 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");
    }
  #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;