Jan 20, 2018 at 12:59 pm Java

How to Check if a Word is a Palindrome in Java

In this post, I will show how to check if a word or sentence is palindrome in Java.

If a word or sentence reads the same backwards or forward, we call it palindrome. For instance, if we reverse the word ‘madam’ we get the same word – ‘madam’. Words such as racecar, rotator, tattarrattat, which is also the longest palindrome in the Oxford English Dictionary according to Wikipedia, are palindrome words.

Using a Java program, we can check if a word is a palindrome. We can at least use two methods to reverse a particular word or string in Java. One method is using Java loop to reverse the word.
The following snippet uses the Java loops control to reverse the word. The program then compares the original and the reversed word.


String orginalWord = "tattarrattat";
String reversedWord = "";
for(int i=orginalWord.length()-1; i >=0; i--){
     reversedWord = reversedWord + orginalWord.charAt(i);
}		
System.out.println("original word " + orginalWord );
System.out.println("Reversed word " + reversedWord );
if(orginalWord.equals(reversedWord)){
    System.out.println("The word is Palindrome");
} else{
    System.out.println("The word is not Palindrome");
}

If you have more than one word with space between them, you need to modify the code to take that into account.

I think the easiest method is using the Java’s StringBuilder class. The StringBuilder class has a reverse method, so we don’t need to use a loop mechanism to reverse the word.

The first thing we need to do is convert the string to the StringBuilder as shown below: String orginalWord = “tattarrattat”;

StringBuilder sb = new StringBuilder(orginalWord);

After that, we can reverse and assign the reversed word to a String variable. Here is the snippet that does just that.

String reversedWord = new String(sb.reverse());

To compare the two words, we can use the String’s equals() method. For example, we can compare the two words like this,
orginalWord.equals(reversedWord)

The above code returns either true or false. Please note we need to either change the words to lowercase or uppercase. So the code below changes the words to lowercase and at the same time compares the two words whether the word reads the same forward or backwards.
orginalWord.toLowerCase().equals(reversedWord.toLowerCase()).

The full code is shown below:


public class Palindrome{
    public static void main(String[] args){

	String orginalWord = "tattarrattat";
	StringBuilder sb = new StringBuilder(orginalWord);

	String reversedWord = new String(sb.reverse());
	System.out.println("original word " + orginalWord );
	System.out.println("Reversed word " + reversedWord );

	if(orginalWord.toLowerCase().equals(reversedWord.toLowerCase())){
	    System.out.println("The word is Palindrome");
	} else {
	    System.out.println("The word is not Palindrome");
	}
     }
}

When you run the code using the particular word,  you will get the following output:

original word tattarrattat
Reversed word tattarrattat
The word is Palindrome

Leave a Reply

Your email address will not be published. Required fields are marked *