Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.

How to Check if a Word is a Palindrome in Java

January 20, 2018

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

29 comments on “How to Check if a Word is a Palindrome in Java

  1. I am extremely impressed with your writing skills and also with the structure on your weblog. Is that this a paid theme or did you customize it yourself? Either way keep up the excellent high quality writing, it’s rare to see a nice blog like this one nowadays..|

  2. Aw, this was a really nice post. In thought I want to put in writing like this additionally ?taking time and actual effort to make a very good article?but what can I say?I procrastinate alot and in no way seem to get one thing done.

  3. Howdy! Would you mind if I share your blog with my myspace group? There’s a lot of folks that I think would really enjoy your content. Please let me know. Cheers|

  4. When I initially commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get four emails with the identical comment. Is there any way you can remove me from that service? Thanks!

  5. Oh my goodness! Incredible article dude! Thanks, However I am going through problems with your RSS. I don’t know the reason why I cannot subscribe to it. Is there anybody getting similar RSS issues? Anybody who knows the solution can you kindly respond? Thanks!!|

  6. You could certainly see your skills in the article you write. The arena hopes for more passionate writers like you who are not afraid to mention how they believe. Always go after your heart.|

  7. Attractive component of content. I just stumbled upon your weblog and in accession capital to say that I acquire in fact loved account your blog posts. Anyway I will be subscribing to your feeds and even I success you get admission to consistently fast.|

  8. I used to be very happy to find this web-site.I needed to thanks in your time for this excellent read!! I positively having fun with each little little bit of it and I’ve you bookmarked to check out new stuff you blog post.

  9. This web site is really a walk-by means of for the entire information you needed about this and didn抰 know who to ask. Glimpse here, and also you抣l positively uncover it.

  10. Can I simply say what a reduction to search out someone who actually knows what theyre talking about on the internet. You positively know the right way to convey a problem to gentle and make it important. Extra individuals need to read this and perceive this facet of the story. I cant believe youre not more popular because you undoubtedly have the gift.

  11. Spot on with this write-up, I really assume this website needs much more consideration. I抣l in all probability be again to read rather more, thanks for that info.

  12. Hey! I just want to give a huge thumbs up for the good information you’ve gotten right here on this post. I will be coming back to your weblog for extra soon.

  13. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!|

  14. I am also writing to make you know what a impressive encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let other folks smoothly know just exactly several impossible subject matter. You truly exceeded our own expectations. Thank you for offering these necessary, safe, explanatory and even fun tips about this topic to Julie.

  15. I am also writing to make you know what a impressive encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let most people smoothly know several impossible subject matter. You truly exceeded our own expectations. Thank you for offering these necessary, safe, explanatory and even fun tips about this topic to Julie.

  16. I precisely had to say thanks all over again. I’m not certain the things I could possibly have created in the absence of the actual hints discussed by you concerning such concern. It actually was a real difficult condition for me personally, but discovering a new professional fashion you handled it forced me to cry for delight. I’m grateful for the assistance and as well , have high hopes you comprehend what a powerful job you’re carrying out educating men and women by way of your blog. I am certain you’ve never come across all of us.

  17. I was curious if you ever considered changing the page layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?|

  18. I simply wanted to write a quick message so as to say thanks to you for the amazing advice you are placing on this site. My extended internet research has at the end been compensated with incredibly good insight to share with my co-workers. I ‘d point out that most of us visitors actually are rather lucky to be in a great website with many marvellous professionals with interesting advice. I feel very much fortunate to have seen the weblog and look forward to really more fun times reading here. Thanks once again for a lot of things.

Leave a Reply

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