Skip to content

Instantly share code, notes, and snippets.

@jongha
Created May 28, 2015 06:04
Show Gist options
  • Save jongha/50c3bb4b727f4b8ca7db to your computer and use it in GitHub Desktop.
Save jongha/50c3bb4b727f4b8ca7db to your computer and use it in GitHub Desktop.
public class Palindrome {
public static void main(String[] args) {
System.out.println(isPalindrome("amor roma"));
System.out.println(isPalindrome("Was it a car or a cat I saw?"));
}
public static Boolean isPalindrome(String text) {
StringBuilder builder = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isAlphabetic(c)) {
builder.append(Character.toLowerCase(c));
}
}
text = builder.toString();
int len = text.length();
for (int i = 0; i < len / 2; ++i) {
if (text.charAt(i) != text.charAt(len - i - 1)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment