-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeChecker.java
More file actions
36 lines (33 loc) · 1.1 KB
/
PalindromeChecker.java
File metadata and controls
36 lines (33 loc) · 1.1 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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class PalindromeChecker {
public static boolean isPalindrome(String input) {
// Create a queue to store characters
Queue<Character> queue = new LinkedList<>();
// Enqueue characters from the input string
for (char c : input.toCharArray()) {
queue.add(c);
}
// Dequeue characters and compare with the end of the string
while (!queue.isEmpty()) {
//king
char front = queue.remove();
char end = input.charAt(input.length() - 1);
if (front != end) {
return false; // Not a palindrome
}
input = input.substring(0, input.length() - 1);
}
return true;
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String input = sc.nextLine();
if (isPalindrome(input)) {
System.out.println(input + " is a palindrome.");
} else {
System.out.println(input + " is not a palindrome.");
}
}
}