Tuesday, October 13, 2015

[7.14,7.15] A palindrome hcecker

1. Implementation
You can reuse your reverse method, leading to a one-line method: The fewer lines of code you need
to write, the fewer places you can introduce bugs. Assuming the reverse method is fully tested,

writing unit tests for this method will be very, very simple

public static boolean isPalindrome(final String s)
{
   
     final String toCheck = s.toLowerCase();
     int left = 0; 
     int right = toCheck.length()-1;



     while (left < right)
     {




          // Not a Character
          while( left < toCheck.length() && !Character.isLetter(toCheck.charAt(i)))
          {
              left++;
          }
          // Not a Character
          while( right > 0 && !Character.isLetter(toCheck.charAt(right)))
          {
              right--;
          }
          // Sanity check since left and right could have been updated
          if ( left > toCheck.length() || right < 0  )
              return false;
           




          if ( toCheck.charAt(left)  != toCheck.charAt(right) )
              return false;




          
          left++;
          right--;




     }



     return true;


}


public static boolean strictPalindorme(final String s)
{
      return s.euqals( reverse(s)  );
}

No comments:

Post a Comment