Let’s learn string palindrome in java using reverse method.
String palindrome in java using reverse method
Here we are using library methods, that is, reverse() function of StringBuffer class.
public class StringPalindromeUsingReverseMethod { public static void main(String[] args) { isPalindrome("mom"); isPalindrome("flowerbrackets"); } public static void isPalindrome(String str) { String strReverse = new StringBuffer(str).reverse().toString(); if(str.equals(strReverse)) { System.out.println("Yes"); } else { System.out.println("No"); } } }
Output:
Yes
No
Also read – ArrayList in java