CareerBuilder Interview Question

Write a function that will determine whether or not a string is a palindrome.

Interview Answer

Anonymous

Feb 25, 2015

public class Palindrome { public static boolean isPalindrome(String s){ int lowerIndex = 0; int upperIndex = s.length() - 1; boolean result = true; String newString = s.toLowerCase().replaceAll(" ", ""); while (upperIndex > lowerIndex){ if (newString.charAt(lowerIndex++) != newString.charAt(upperIndex--)){ result = false; break; } } return result; } public static void main(String[] args){ System.out.println("not a palindrome: " + isPalindrome("notapalindrome")); System.out.println("Palindrome: " + isPalindrome("appaappa")); System.out.println("Palindrome: " + isPalindrome("apaapa")); } }