A program to check String is palindrom or not?

Posted by
 Palindrom string is the string which gives the same result when reversed as original string.   for example: Nitin.

Code:

 class checkPalindrom  
 {  
   public static void main(String a[])  
   {  
     //command line argumennt  
     String str=a[0];  
     StringBuilder temp=null;  
     String arr[]=str.split(" ");  
     if (arr.length==1)  
     {  
       temp=new StringBuilder(arr[0]);  
       temp.reverse();  
       if(str.equals(temp.toString()))  
       System.out.println("String is palindrom");  
       else  
       System.out.println("Sting is not palindrom");  
     }  
     else  
     {  
       for(int i=arr.length-1;i>=0;i--)  
       {  
         temp=new StringBuilder(arr[i]);  
         temp.reverse();   
         temp.append(temp);   
       }  
       if(str.equals(temp.toString()))  
       System.out.println(" String is palindrom");  
       else  
       System.out.println("String is not palindrom");  
     }  
   }  
 }  

Output:


You may also like following string programs:

  1. Java program to find the first letter alphabetically in String
  2. Program to remove duplicate character from string without loosing the order in which it inserted
  3. A program to reverse a string and change first character case to lower case and last to lower case.
  4.  A program to reverse a String (Without using API)

Leave a Reply