Saturday, December 5, 2009

WAP to check whether string is palindrome

Problem
Write a method which will accept a string and return true if the string is a palindrome and false if it isn't.

Follow up:
a) your method should consider lower case and upper case characters to be the same.
b) your method should ignore special characters and white spaces, for e.g. if your input were the strings were "Madam, I'm Adam!!", then you should consider it a palindrome and hence return true ignoring case and special characters. Same with inputs like "Ma'am", "boB" etc should return true.

Solution
Considering the simple case
Eg, of pallindrome = Malayalam
void isPalindrome(char *string)
{
  char *start, *end;

  if(string)
  {
     start = string;
     end   = string + strlen(string) - 1;

     while((*start == *end) && (start!=end))
     {
       if(start
       if(end>start)end--;
     }

     if(*start!=*end)
     {
        printf("\n[%s] - This is not a palidrome!\n", string);
     }
     else
     {
        printf("\n[%s] - This is a palidrome!\n", string);
     }
  }
  printf("\n\n");
}
Answering the following up questions , to come soon.


0 comments:

Post a Comment