In this section, we will find palindrome of a Java string. It works in the same way as that of integers, For example, "madam" is a palindrome, but "madame" is not a palindrome. Let's implement this palindrome program in Java using string reverse function.
class PalindromeProgram
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// checks whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes, it is a palindrome");
else
System.out.println("No, it is not a palindrome");
}
public static void main (String[] args)
throws java.lang.Exception
{
checkPalindrome("madam");
}
}
Output: Yes, it is a palindrome
Explanation: In the above code, we have used string reverse function to calculate the reverse of a number and then compare the same with the original number. If both the numbers are same, it will print palindrome number, else not a palindrome number.