-
Notifications
You must be signed in to change notification settings - Fork 105
/
Palindrome.easy
34 lines (27 loc) · 908 Bytes
/
Palindrome.easy
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
//code for checking that given string is palindrome or not
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String input = scanner.nextLine();
if (isPalindrome(input)) {
System.out.println("It's a palindrome!");
} else {
System.out.println("It's not a palindrome.");
}
}
public static boolean isPalindrome(String str) {
str = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}