开发者

How do I detect a palindrome in C?

开发者 https://www.devze.com 2022-12-22 00:04 出处:网络
I\'ve been working through potential interview questions and one of them was to write a function in C to detect whether a given string was a palindrome or not.

I've been working through potential interview questions and one of them was to write a function in C to detect whether a given string was a palindrome or not.

I've gotten a pretty good start on it:

#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 + strle开发者_开发问答n(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");
    }
}

However, I now want to ensure that I ignore spaces and punctuation.

What is the best way, given the code I have written above, to advance the pointers forward or backward should they encounter punctuation/spaces?


change the loop to

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


Inside the while loop, just skip over any characters you want to ignore:

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

   // and something similar for end

One other comment. Since your function is not modifying the parameter, you should define it as:

bool isPalindrome(const char *value);


/* 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");
    }


How about writing another function to remove the space and punctuation chars in a string?


Refer this following example to check the whether the string is palindrome

 

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 ;
}


Here's my take on it, tried to be concise. Also, just added check for no input

#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");
}


  #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;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消