-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.java
More file actions
27 lines (20 loc) · 834 Bytes
/
Copy pathpalindrome.java
File metadata and controls
27 lines (20 loc) · 834 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
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Input
System.out.print("Enter a string to check if it's a palindrome: ");
String input = scanner.nextLine();
// Step 2: Preprocess (optional: remove spaces, make lowercase)
String original = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Step 3: Reverse the string
String reversed = new StringBuilder(original).reverse().toString();
// Step 4: Check and output
if (original.equals(reversed)) {
System.out.println("✅ It's a palindrome!");
} else {
System.out.println("❌ It's not a palindrome.");
}
scanner.close();
}
}