Search code examples
c++palindrome

C++ How to ignore the difference between upper and lower when checking Palindrome?


bool isPalindrome(const char *s1)
{

  const char *start, *end;

  start = s1;
  end = s1 + strlen(s1) - 1;


  while (start < end)
      if(*start++ != *end--)
        return false;
  return true; 
}

I write the code about checking Palindrome, but ignore the requirement of ignoring difference between upper and lower. I have no idea about that. Can you help me? Thanks!


Solution

  • There is a function called "tolower" which will take an upper case letter and convert it to a lowercase letter. It will return the value unchanged if it was already lowercase or it was not a letter. Your code as written is very close to being entirely correct and only needs a slight change.

    if(*start++ != *end--)
    

    will become

    if(tolower(*start++) != tolower(*end--))