Software & Finance





Java - Palindrome of a given String





Here to the Java Source on how to check a string is a Palindome or not.

 

Source Code


import java.io.*;

 

 

class Palindrome {     

 

    public static void main(String[] args) {

 

            String inpstring = "";

            InputStreamReader input = new InputStreamReader(System.in);

            BufferedReader reader = new BufferedReader(input);

 

            try

            {

                  System.out.print("Enter a string to check Palindrome:");

                  inpstring = reader.readLine();

 

                  int len = inpstring.length();

                  inpstring = inpstring.toUpperCase();

                  boolean bPalindrome = true;

 

                  for (int i = 0; i < (len / 2 + 1); i++)

                  {

                        if (inpstring.charAt(i) != inpstring.charAt(len - i - 1))

                        {

                              bPalindrome = false;

                        }

                  }

                  if(bPalindrome == true)

                        System.out.println(inpstring + " is a palindrome");

                  else

                        System.out.println(inpstring + " is NOT a palindrome");

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

    }

}

 

Output


C:\Java\Samples>javac Palindrome.java

 

C:\Java\Samples>java Palindrome

Enter a string to check Palindrome:Kathiresan

KATHIRESAN is NOT a palindrome

 

C:\Java\Samples>java Palindrome

Enter a string to check Palindrome:Malayalam

MALAYALAM is a palindrome