-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeChecker.java
More file actions
44 lines (36 loc) · 1.42 KB
/
PalindromeChecker.java
File metadata and controls
44 lines (36 loc) · 1.42 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
39
40
41
42
43
44
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("How many times would you like to try? Type 3: ");
int attempts = scanner.nextInt();
scanner.nextLine(); // Consume newline
for (int i = 0; i < attempts; i++) {
System.out.print("Enter a string (more than 10 characters): ");
String input = scanner.nextLine();
if (input.length() <= 10) {
System.out.println("Input must have more than 10 characters.");
continue;
}
if (isPalindrome(input)) {
System.out.println("The string '" + input + "' is a palindrome.");
} else {
System.out.println("The string '" + input + "' is not a palindrome.");
}
}
scanner.close();
}
public static boolean isPalindrome(String str) {
str = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); // Remove non-alphanumeric characters and convert to lowercase
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}