Java code to check the given string is palindrome or not

Here we are going to check whether the given string value is palindrome or not. We will declare two String as "original" and "reverse". Then import the Scanner object which will enable us to enter any String value into the console. In my example I'm going to enter "mom" as an original String and which is obviously a palindrome.

Palindrome definition:

The string value should be the same whether we read from left hand side or from right hand side.
Palindrome exp: mom, dad
Non palindrome exp: java, string etc.

import java.util.*;
public class PalindromeTest {
    public static void main(String args[]) {         String original, reverse = "";         Scanner in = new Scanner(System.in);         System.out.print("Enter a string to check if it is a palindrome: ");         original = in.nextLine();         int length = original.length();         for (int i = length - 1; i >= 0; i--)             reverse = reverse + original.charAt(i);         if (original.equals(reverse))             System.out.println("Entered string is a palindrome.");         else             System.out.println("Entered string is not a palindrome.");     } }

Output:

Enter a string to check if it is a palindrome: mom
Entered string is a palindrome.

7 comments:

  1. Replies
    1. Case senstive objects ... so in Adam... A is capital so holds different value

      Delete
  2. Replies
    1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

      or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

      Delete
  3. Really Nice Information,Thank You Very Much For Sharing.
    Web Development Company

    ReplyDelete
  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java EE Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete