JAVA Program to Check a Palindrome String

To find out whether a given String is a Palindrome or not is among one of the very basic problems of coding in various languages including JAVA, C, C++ etc. A string is said to be Palindrome string if it has the beginning words and ending words same characters or we can also say that a Palindrome string is a string which may be read the same way in either direction i.e. from first character to last character or last character to first character. The few of the examples of a palindrome string are Madam, Naman, ADA, DAD, Radar etc.

JAVA Program to check Palindrome String

import java.util.*;

class Pal
{
   public static void main(String args[])
   {
      String original, reverse="";
      Scanner in = new Scanner(System.in);

      System.out.println("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 of Java Program for Checking a Palindrome String

Enter a string to check if it is a palindrome
Mridul
Entered string is not a palindrome.

CMD Screen Output of Palindrome Program




0 comments:

Post a Comment