Wednesday, July 3, 2024
HomeLanguagesJavaJava program to check palindrome (using library methods)

Java program to check palindrome (using library methods)

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 : Lazyroar
Output : No

Java




// Java program to illustrate checking of a string 
// if its palindrome or not using reverse function 
import java.io.*;
public 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("Lazyroar"); 


Output

Yes
No

Time complexity: O(N), where N is length of given string.
Auxiliary Space: O(N), The extra space is used to store the reverse of the string.

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 Lazyroar and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the Lazyroar 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.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments