Friday, November 12, 2010

is a string palindrome

how to find that a given string is palindrome given a string can contain space as well. we don;t need to consider space. eg: a cd ef ed ca is a palindrome
bool isPalindrome(char *str)
{
    if(!str)
    return false;
   int i = 0, j = strlen(str)-1;
   while(i<j)
  {
     if(str[i]==" ")
        i++;
    if(str[j]==" ")
      j--;
   if(str[i]!=" "&&str[j]!=" ")
    {
            if(str[i]==str[j])
            {
                i++;
                j--;
            }
           else
             return false;
    }
}
   if(i>=j)
      return true;
}

No comments:

Post a Comment