Skip to content

Instantly share code, notes, and snippets.

@ricky-lim
Last active March 30, 2022 11:16
Show Gist options
  • Save ricky-lim/c5f0caf826d2ee9297d65a582235bbc8 to your computer and use it in GitHub Desktop.
Save ricky-lim/c5f0caf826d2ee9297d65a582235bbc8 to your computer and use it in GitHub Desktop.
Palindrom checker
import java.util.stream.IntStream;
class Scratch {
public static boolean palindromeChecker(String s) {
String normalizedString = s.trim().toLowerCase();
return IntStream.range(0, normalizedString.length() / 2)
.allMatch(i ->
normalizedString.charAt(i) ==
normalizedString.charAt(normalizedString.length() - i - 1));
}
public static void main(String[] args) {
// Palindrome
String s1 = "civic";
String s2 = "maDam";
String s3 = "aBBa";
// Non-palindrome
String s4 = "camCard";
String s5 = "Ricky";
System.out.println(palindromeChecker(s1));
System.out.println(palindromeChecker(s2));
System.out.println(palindromeChecker(s3));
System.out.println(palindromeChecker(s4));
System.out.println(palindromeChecker(s5));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment