- Yogeeta Paryani
JAVA | Check whether given String is Palindrome or not
To check whether the string is palindrome or not, we have to compared the string with the reverse of itself.
For Example 'LOL' is a palindrome string as position of each character remain same after reversing it.
Program:
import java.util.Scanner;
class ChkPalindrome
{
public static void main(String args[])
{
//Declaring temporary string
String rev="";
Scanner in=new Scanner(System.in);
//Taking original String from user
System.out.println("Enter a String to check:");
String str=in.nextLine();
// Storing length of String
int n=str.length();
//Reversing the String and storing into rev
for(int i=n-1;i>=0;i--)
{
rev=rev+str.charAt(i);
}
//Now comparing whether str is equal to rev or not
/*using equalsIgnoreCase string method to ignoring the case considerations*/
if(str.equalsIgnoreCase(rev))
{
System.out.println( str+" is a palindrome.");
}
else
{
System.out.println(str+" is not a palindrome.");
}
}
}
Output:
Enter a String to check:
LOL
LOL is a palindrome.
Enter a String to check:
GET
GET is not a palindrome.
Happy Coding!
Follow us on Instagram @programmersdoor
Join us on Telegram @programmersdoor
Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
Follow Programmers Door for more.
#blog #strings #java #palindrome #learn #programming
52 views0 comments