-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeChecker.java
More file actions
29 lines (22 loc) · 824 Bytes
/
Copy pathPalindromeChecker.java
File metadata and controls
29 lines (22 loc) · 824 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
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word or phrase: ");
String input = sc.nextLine();
// Remove spaces and punctuation, convert to lowercase
String cleaned = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
String reversed = "";
// Reverse the string
for (int i = cleaned.length() - 1; i >= 0; i--) {
reversed += cleaned.charAt(i);
}
// Check if palindrome
if (cleaned.equals(reversed)) {
System.out.println("The given input is a Palindrome.");
} else {
System.out.println("The given input is NOT a Palindrome.");
}
sc.close();
}
}