forked from ravya1108/hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Palindrome Checker fixes ravya1108#192
- Loading branch information
1 parent
1c13757
commit 1095b83
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |