Core Java

Palindrome Java Program

Photo of Nikos MaravitsasNikos MaravitsasJuly 22nd, 2014Last Updated: August 4th, 2020
0 298 3 minutes read

In this example, we are going to see a Palindrome Java Program, to check whether a String is a palindrome. AString 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. Palindrome Java Program – Simple approach

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

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

PalindromeExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
packagecom.javacodegeeks.core.palindrome;
 
publicclassPalindromeExample {
 
    privatestaticfinalString STR1 ="abbcbba";
    privatestaticfinalString STR2 ="isdovosjd";
 
    publicstaticvoidmain(String[] args) {
         
         
        System.out.println("String :"+STR1+" is a palindrome :"+PalindromeExample.isPalindrome(STR1));
        System.out.println("String :"+STR2+" is a palindrome :"+PalindromeExample.isPalindrome(STR2));
 
    }
 
    publicstaticbooleanisPalindrome(String str){
 
        String reverse =newStringBuffer(str).reverse().toString();
         
        if(reverse.equals(str))
            returntrue;
         
        returnfalse;
    }
}

Theoutput of the program is:

1
2
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 aString requires double the memory along with some coping, and of course the additional computations of theequals 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 theString is “mirrored” by the other half. This means that the first half of theString 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 theString is not a palindrome.
  4. If the two pointers meet half-way, then theString is a palindrome.

PalindromeExample.java

01
02
03
04
05
06
07
08
09
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
packagecom.javacodegeeks.core.palindrome;
 
publicclassPalindromeExample {
 
    privatestaticfinalString STR1 ="uabbcbbau";
    privatestaticfinalString STR2 ="isdovosjd";
 
    publicstaticvoidmain(String[] args) {
        System.out.println("String :"+STR1+" is a palindrome :"+PalindromeExample.isPalindrome2(STR1));
        System.out.println("String :"+STR2+" is a palindrome :"+PalindromeExample.isPalindrome2(STR2));
    }
 
    publicstaticbooleanisPalindrome(String str){
 
        String reverse =newStringBuffer(str).reverse().toString();
         
        if(reverse.equals(str))
            returntrue;
         
        returnfalse;
    }
     
    publicstaticbooleanisPalindrome2(String str){
 
        intstart =0;
        intend = str.length() -1;
        inthalf = end/2;
         
        for(inti =0; i < half; i++, start++, end-- ){
            if(str.charAt(start) != str.charAt(end))
                returnfalse;
        }
         
        returntrue;
    }
}

Theoutput of the program is:

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

It’s fairly simple.

3. 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:Palindrome Java Program

Last updated on May 4th, 2020

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Nikos MaravitsasNikos MaravitsasJuly 22nd, 2014Last Updated: August 4th, 2020
0 298 3 minutes read
Photo of 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.

Related Articles

Bipartite Graph

Java not equal Example

January 17th, 2020
Bipartite Graph

Java API Tutorial

October 26th, 2020
Bipartite Graph

Java Struct Example

January 8th, 2020
Bipartite Graph

Java Node Example

November 20th, 2019
Bipartite Graph

Java Swing MVC Example

January 26th, 2016
Bipartite Graph

How to call a method in Java

December 26th, 2019
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.