Advertisement
Guest User

Check If Palindrome

a guest
Jan 11th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. public static boolean palindrome(String word)
  2. {
  3.     word = word.toLowerCase().replaceAll(" ", ""); // remove spaces and make case-insensitive
  4.     boolean isPalindrome = true;
  5.    
  6.     for (int i = 0; isPalindrome && i < word.length() / 2; i++)
  7.     {
  8.         isPalindrome = (word.charAt(i) == word.charAt(word.length() - i - 1));
  9.     }
  10.    
  11.     return isPalindrome;
  12. }
  13.  
  14. /* You can call the method like so, wherever you choose: */
  15.  
  16. if (palindrome("racecar"))
  17.     System.out.println("'racecar' is a palindrome!");
  18. else
  19.     System.out.println("not a palindrome :(");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement