Skip to content
Related Articles

Related Articles

Java program to check palindrome (using library methods)
  • Difficulty Level : Basic
  • Last Updated : 05 May, 2017

Given a string, write a Java function to check if it is palindrome or not. A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome, but “abbc” is not palindrome. The problem here is solved using string reverse function.

Examples:

Input : malayalam
Output : Yes

Input : GeeksforGeeks
Output : No




// Java program to illustrate checking of a string
// if its palindrome or not using reverse function
class Palindrome
{
  public static void checkPalindrome(String s)
  {
    // reverse the given String
    String reverse = new StringBuffer(s).reverse().toString();
  
    // check whether the string is palindrome or not
    if (s.equals(reverse))
      System.out.println("Yes");
  
    else
      System.out.println("No");
  }
  
  public static void main (String[] args)
               throws java.lang.Exception
  {
    checkPalindrome("malayalam");
    checkPalindrome("GeeksforGeeks");
  }
}

Output:

Yes
No

Related Article :
C program to check whether a given string is palindrome or not



This article is contributed by Bhargav Sai Gajula. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :