Home » Core Java » Palindrome Program in Java

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Palindrome Program in Java

In this example we are going to see how you can create a simple Java Application to check weather a String is a palindrome. A String is considered a palindrome if it can be similarly read both from left to right and from right to left. For example "abbcbba", “12321”, “69796” are all palindromes.

1. A Simple approach

It’s very easy to think of a simple algorithm that can check if a String is a palindrome. You take the String, let’s say , "abbcbba", and reverse it : "abbcbba". Exactly the same. On the other hand, if you reverse a non-palindrome String, let’s say "java", you came up with "avaj".

So a very effective first approach would be to take the initial String, reverse it and check if the result String is equal to the original. If it is, then our input String is a palindrome, else it’s not.

PalindromeExample.java:

package com.javacodegeeks.core.palindrome;

public class PalindromeExample {

	private static final String STR1 = "abbcbba";
	private static final String STR2 = "isdovosjd";

	public static void main(String[] args) {
		
		
		System.out.println("String :"+STR1+" is a palindrome :"+PalindromeExample.isPalindrome(STR1));
		System.out.println("String :"+STR2+" is a palindrome :"+PalindromeExample.isPalindrome(STR2));

	}

	public static boolean isPalindrome(String str){

		String reverse = new StringBuffer(str).reverse().toString();
		
		if (reverse.equals(str))
			return true;
		
		return false;
	}
}

The output of the program is:

String :abbcbba is a palindrome :true
String :isdovosjd is a palindrome :false

Of course, there are many ways you can compute the reverse of a given String. and you can find a good guide that lists different approaches, in this example : Java String reverse Example.

2. A more efficient algorithm

It’s not hard to see that in the above example, reversing a String requires double the memory along with some coping, and of course the additional computations of the equals method. Non of the above are particularly costly, but you could think of a more efficient and faster solution.

Our second approach is based on the intuition that a String is a palindrome if the first half of the String is “mirrored” by the other half. This means that the first half of the String read from left to right, is the same as the second half read from right to left.

If you imagine the String as a char array, then it’s straightforward to implement an algorithm that takes advantage of that idea:

  1. Have a pointer pointing to the first element of the array, and a second pointer pointing at the last element of the array.
  2. The first pointer will advance from left to right, and the second one from right to left.
  3. At each step, check if the characters pointed by the two pointers are the same. If they are, move the first pointer one position to the right and the second pointer on position to the left. If the are not equal, then the String is not a palindrome.
  4. If the two pointers meet half-way, then the String is a palindrome.

PalindromeExample.java:

package com.javacodegeeks.core.palindrome;

public class PalindromeExample {

	private static final String STR1 = "uabbcbbau";
	private static final String STR2 = "isdovosjd";

	public static void main(String[] args) {
		System.out.println("String :"+STR1+" is a palindrome :"+PalindromeExample.isPalindrome2(STR1));
		System.out.println("String :"+STR2+" is a palindrome :"+PalindromeExample.isPalindrome2(STR2));
	}

	public static boolean isPalindrome(String str){

		String reverse = new StringBuffer(str).reverse().toString();
		
		if (reverse.equals(str))
			return true;
		
		return false;
	}
	
	public static boolean isPalindrome2(String str){

		int start = 0;
		int end = str.length() - 1;
		int half = end/2;
		
		for(int i = 0; i < half; i++, start++, end-- ){
			if(str.charAt(start) != str.charAt(end))
				return false;
		}
		
		return true;
	}
}

The output of the program is:

String :uabbcbbau is a palindrome :true
String :isdovosjd is a palindrome :false

It's fairly simple.

Download the Source Code

This was an example on developing a Palindrome Program in Java.

Download
You can download the full source code of this example here : JavaPalindromeExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close