diff --git a/src/main/java/com/thealgorithms/others/EvenOddChecker.java b/src/main/java/com/thealgorithms/others/EvenOddChecker.java new file mode 100644 index 000000000000..8c32d41b7c90 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/EvenOddChecker.java @@ -0,0 +1,12 @@ +package com.thealgorithms.others; + +public class EvenOddChecker { + public static String checkEvenOdd(int number) { + return number % 2 == 0 ? "Even" : "Odd"; + } + + public static void main(String[] args) { + int test = 7; + System.out.println(test + " is " + checkEvenOdd(test)); + } +} diff --git a/src/main/java/com/thealgorithms/strings/PalindromeChecker.java b/src/main/java/com/thealgorithms/strings/PalindromeChecker.java new file mode 100644 index 000000000000..39715c15c83e --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/PalindromeChecker.java @@ -0,0 +1,41 @@ +package com.thealgorithms.strings; + +/** + * Utility class to check if a string is a palindrome. + */ +public class PalindromeChecker { + + /** + * Checks if the given input is a palindrome. + * Ignores spaces, punctuation, and case. + * + * @param input the string to check + * @return true if it's a palindrome, false otherwise + */ + public static boolean isPalindrome(String input) { + if (input == null) { + return false; + } + // Remove non-letter characters and convert to lowercase + String cleaned = input.replaceAll("[^a-zA-Z]", "").toLowerCase(); + int left = 0; + int right = cleaned.length() - 1; + + while (left < right) { + if (cleaned.charAt(left) != cleaned.charAt(right)) { + return false; + } + left++; + right--; + } + + return true; + } + + public static void main(String[] args) { + String test1 = "A man a plan a canal Panama"; + String test2 = "OpenAI"; + System.out.println("\"" + test1 + "\" is palindrome? " + isPalindrome(test1)); + System.out.println("\"" + test2 + "\" is palindrome? " + isPalindrome(test2)); + } +}