File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class PalindromeChecker {
4+
5+ public static boolean isPalindrome (String str ) {
6+ str = str .replaceAll ("[^a-zA-Z0-9]" , "" ).toLowerCase (); // Remove non-alphanumeric characters and convert to lowercase
7+ int left = 0 ;
8+ int right = str .length () - 1 ;
9+
10+ while (left < right ) {
11+ if (str .charAt (left ) != str .charAt (right )) {
12+ return false ; // Characters at the left and right positions don't match
13+ }
14+ left ++;
15+ right --;
16+ }
17+
18+ return true ; // All characters matched, it's a palindrome
19+ }
20+
21+ public static void main (String [] args ) {
22+ Scanner scanner = new Scanner (System .in );
23+
24+ System .out .print ("Enter a string: " );
25+ String input = scanner .nextLine ();
26+
27+ if (isPalindrome (input )) {
28+ System .out .println ("The input is a palindrome." );
29+ } else {
30+ System .out .println ("The input is not a palindrome." );
31+ }
32+
33+ scanner .close ();
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments