-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion22.java
More file actions
31 lines (25 loc) · 942 Bytes
/
question22.java
File metadata and controls
31 lines (25 loc) · 942 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
30
31
import java.util.Scanner;
public class question22 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("\"" + input + "\" is a palindrome.");
} else {
System.out.println("\"" + input + "\" is not a palindrome.");
}
scanner.close();
}
public static boolean isPalindrome(String str) {
StringBuilder cleaned = new StringBuilder();
for (char ch : str.toCharArray()) {
if (Character.isLetterOrDigit(ch)) {
cleaned.append(Character.toLowerCase(ch));
}
}
String forward = cleaned.toString();
String reversed = cleaned.reverse().toString();
return forward.equals(reversed);
}
}