-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalidrome2.java
More file actions
28 lines (23 loc) · 956 Bytes
/
Palidrome2.java
File metadata and controls
28 lines (23 loc) · 956 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
package lab6;
import java.util.Scanner;
public class Palidrome2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String userInput = scanner.nextLine();
if (isPalindrome(userInput)) {
System.out.println(userInput + " is a palindrome.");
} else {
System.out.println(userInput + " is not a palindrome.");
}
scanner.close();
}
public static boolean isPalindrome(String input) {
// Remove all non-alphanumeric characters and convert to lowercase
String cleanInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Reverse the string
String reversed = new StringBuilder(cleanInput).reverse().toString();
// Check if the original string is equal to its reverse
return cleanInput.equals(reversed);
}
}