-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeChecker79.java
37 lines (31 loc) · 1.23 KB
/
PalindromeChecker79.java
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
public class PalindromeChecker79 {
// Method to check if a string is a palindrome
public static boolean isPalindrome(String str) {
// Remove spaces and convert to lowercase for case-insensitive comparison
String processedStr = str.replaceAll("\\s+", "").toLowerCase();
// Check if the string is a palindrome
int left = 0;
int right = processedStr.length() - 1;
while (left < right) {
if (processedStr.charAt(left) != processedStr.charAt(right)) {
return false; // Characters at left and right positions are different
}
left++;
right--;
}
return true; // String is a palindrome
}
// Main method to execute the program
public static void main(String[] args) {
// Define a string to check for palindrome
String str = "Madam";
// Call the isPalindrome method and store the result
boolean isPal = isPalindrome(str);
// Display the result
if (isPal) {
System.out.println("\"" + str + "\" is a palindrome.");
} else {
System.out.println("\"" + str + "\" is not a palindrome.");
}
}
}