Here's An Example Of A Java Program That Checks If A Given String Is A Palindrome:
```Java
Import Java.Util.Scanner;
Public Class Palindromeprogram {
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("The String Is A Palindrome.");
} Else {
System.Out.Println("The String Is Not A Palindrome.");
}
}
Public Static Boolean Ispalindrome(String Input) {
// Remove All Non-alphanumeric Characters And Convert To Lowercase
String Cleaninput = Input.Replaceall("[^A-za-z0-9]", "").Tolowercase();
Int Left = 0;
Int Right = Cleaninput.Length() - 1;
While (Left < Right) {
If (Cleaninput.Charat(Left) != Cleaninput.Charat(Right)) {
Return False;
}
Left++;
Right--;
}
Return True;
}
}
```
In This Program, We First Take A String Input From The User. Then We Call The `Ispalindrome` Function To Check If The Input String Is A Palindrome. The `Ispalindrome` Function Removes All Non-alphanumeric Characters And Converts The String To Lowercase To Ignore Case Sensitivity. It Then Uses Two Pointers, `Left` And `Right`, Which Start From The Beginning And End Of The String, Respectively, And Iteratively Compare The Characters At These Positions. If At Any Point The Characters Don't Match, The Function Returns `False` Indicating That The String Is Not A Palindrome. If The Pointers Meet Or Cross Each Other, It Means All The Characters Have Been Matched, And The Function Returns `True`, Indicating That The String Is A Palindrome.
Note: This Implementation Considers Alphanumeric Characters Only And Ignores Spaces And Special Characters. If You Want To Consider Spaces And Special Characters As Well, You Can Remove The `Replaceall` Line In The `Ispalindrome` Function.

No comments:
Post a Comment