String Palindrome in Java

Today we will see a program where we will find String palindrome in Java programming language.

We have already learned how to find palindrome of a number in one of my Java article. Now in this article, we are going to learn about a new thing. So let’s begin…

What is a String Palindrome?

A string palindrome is a string which is the same when we read it either from front direction or backward direction.

Some examples are NITIN, NAMAN, MADAM, CIVIC, etc. You will able to find lots of other meaningful words like these which will remain the same even if you read it from backward.

In our program what we will do is compare the first and last character of the input string until we reach the half-length of the string.

Consider the string “NITIN”.

We will compare index 0 with 4, 1 with 3,2 with 2. (2 with 2 can be ignored).

So let’s go straight to our Java code. Below is our code:

public class MyClass
    public static void main(String args[])
        String str="abba";
        int ln=str.length();
        int flag=1;
        for(int i=0;i<ln/2;i++)
            System.out.println("Not Palindrome");

Java CharAt function

This Java CharAt function returns the element at the index passed as a parameter.

This code can work both for even numbered and odd numbered string.

Hope this helped you out.

I hope you have understood String Palindrome in Java. Have a nice day ahead and happy coding.

Also, read:

How to find the difference or gap of days between two dates in python

Find the next greater number from the same set of digits in C++

How to copy data from one excel sheet to another using python


Leave a Reply

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