0% found this document useful (0 votes)
10 views1 page

Import Java - Util.scanner

Uploaded by

sandeep12rv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Import Java - Util.scanner

Uploaded by

sandeep12rv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;

public class PalindromeCheck {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");


String original = scanner.nextLine();

String cleanedString = original.replaceAll("\\s+",


"").toLowerCase();

if (isPalindrome(cleanedString)) {
System.out.println("\"" + original + "\" is a palindrome.");
} else {
System.out.println("\"" + original + "\" is not a palindrome.");
}

scanner.close();
}

public static boolean isPalindrome(String str) {


int left = 0;
int right = str.length() - 1;

while (left < right) {


if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}

return true;
}
}

You might also like