Skip to content

Commit

Permalink
Palindrome Checker fixes ravya1108#192
Browse files Browse the repository at this point in the history
  • Loading branch information
th-shristi authored Oct 6, 2023
1 parent 1c13757 commit 1095b83
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Palindrome Checker
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Scanner;

public class PalindromeChecker {

// Function to check if a string is a palindrome
public static boolean isPalindrome(String str) {
// Remove spaces and convert to lowercase for a case-insensitive check
str = str.replaceAll("\\s+", "").toLowerCase();

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

while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false; // Not a palindrome
}
left++;
right--;
}

return true; // if palindrome
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();

if (isPalindrome(input)) {
System.out.println("Yes,it is a palindrome.");
} else {
System.out.println("No,not a palindrome.");
}

scanner.close();
}
}

0 comments on commit 1095b83

Please sign in to comment.