Frequently Asked Java Program 03: Java Program to check if any string is palindrome Using inbuilt Reverse method of Java
Last updated on September 20th, 2018 at 08:34 pm
Hello Folks,
As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a Java program to verify if a given string is palindrome using inbuilt reverse method.
WHAT IS PALINDROME STRING?
A palindromic string is a string that remains the same when its characters are reversed.
For Example: NAAN, AMMA etc.
Logic:
- Java provides two classes StringBuilder and StringBuffer who has reverse() method. Note here that String class has no reverse method.
- We can convert any string to StringBuilder or StringBuffer using their constructors.
- There is a trick to compare content of two objects of StringBuffer or StringBuilder which is discussed in below program.
Java Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package JavaPrograms; import java.util.Scanner; public class PalindromeCharStringUsingStringBuffer { public static void main(String[] args) { // Taking input from user Scanner sc = new Scanner(System.in); System.out.println("Please enter the string to know palindrome:"); String inputByUser = sc.nextLine(); //Converting String in to StringBuilder StringBuilder strOriginal = new StringBuilder(inputByUser); // reversing string StringBuilder strReverse = new StringBuilder(strOriginal).reverse(); //since stringBuildr/Buffer do not override equals method so it will not check content //To verify content we can use valueOf method of String class. This method class toString() internally. if (String.valueOf(strOriginal).equals(String.valueOf(strReverse))) System.out.println("Result:Palindrome"); else System.out.println("Result:Not Palindrome"); //Another way to verify content using toString() if ((strOriginal.toString()).equals(strReverse.toString())) System.out.println("Result:Palindrome"); else System.out.println("Result:Not Palindrome"); //Using compareTo method if ((strOriginal.toString()).compareTo(strReverse.toString()) == 0) System.out.println("Result:Palindrome"); else System.out.println("Result:Not Palindrome"); } } |
Output:
Please enter the string to know palindrome: AMMA Result:Palindrome Result:Palindrome Result:Palindrome Please enter the string to know palindrome: NANA Result:Not Palindrome Result:Not Palindrome Result:Not Palindrome Please enter the string to know palindrome: AMMA AMMA Result:Palindrome Result:Palindrome Result:Palindrome
You can run above program for multiple inputs and if it fails for any condition, let me know.
#HappyCoding
Author: Amod Mahajan
My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
We can also use below approach:
public static void main(String[] args) {
System.out.println(“Enter the input string”);
Scanner sc = new Scanner(System.in);
String inputString = sc.next();
sc.close();
String temp = inputString;
StringBuilder str = new StringBuilder(temp).reverse();
if (inputString.equals(str.toString()))
{
System.out.println(“Is palindrome using StringBuilder”);
}
else {
System.out.println(“String is not Palindrome !!”);
}
}