-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalidrome.java
More file actions
38 lines (30 loc) · 1.07 KB
/
Palidrome.java
File metadata and controls
38 lines (30 loc) · 1.07 KB
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
31
32
33
34
35
36
37
38
package lab6;
import java.util.Scanner;
public class Palidrome {
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();
int left = 0;
int right = cleanInput.length() - 1;
while (left < right) {
// Compare characters from both ends
if (cleanInput.charAt(left) != cleanInput.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}