forked from dimpeshmalviya/JavaBasicPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeChecker.java
More file actions
30 lines (25 loc) · 802 Bytes
/
PalindromeChecker.java
File metadata and controls
30 lines (25 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner;
public class PalindromeChecker {
public static boolean isPalindrome(String str) {
str = str.replaceAll("\\s+", "").toLowerCase();
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word or phrase: ");
String input = sc.nextLine();
if (isPalindrome(input))
System.out.println("✅ It's a palindrome!");
else
System.out.println("❌ Not a palindrome.");
sc.close();
}
}