to find if a given string is palindrome or is not palindrome

前端 未结 9 574
悲哀的现实
悲哀的现实 2020-11-30 15:36

I made a program to find if a entered string is palindrome or not palindrome but it always says that its not a palindrome

#include  
#include          


        
9条回答
  •  无人及你
    2020-11-30 16:01

    From the 2005 version of myself:

    bool isAlphaNumeric(char c)
    {
        return (iswalpha(c) || iswdigit(c));
    }
    
    bool isPalindrome(char *str)
    {
        /* A man, a plan, Anal Panama!!! */
        if(*str == '\0')
        {
            return false;
        }
    
        int len = strlen(str);
        if(len <= 1) return true;
    
        char *start = str;
        char *end = start + len - 1;
    
        while(start < end)
        {
            if(!isAlphaNumeric(*start))
            {
                *start++;
                continue;
            }
            if(!isAlphaNumeric(*end))
            {
                *end--;
                continue;
            }
            if(towlower(*start) != towlower(*end))
            {
                return false;
            }
            *start++;
            *end--;
        }
        return true;
    }
    

提交回复
热议问题