Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. 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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close