Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean palindrome(String word)
- {
- word = word.toLowerCase().replaceAll(" ", ""); // remove spaces and make case-insensitive
- boolean isPalindrome = true;
- for (int i = 0; isPalindrome && i < word.length() / 2; i++)
- {
- isPalindrome = (word.charAt(i) == word.charAt(word.length() - i - 1));
- }
- return isPalindrome;
- }
- /* You can call the method like so, wherever you choose: */
- if (palindrome("racecar"))
- System.out.println("'racecar' is a palindrome!");
- else
- System.out.println("not a palindrome :(");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement