http://www.technicalpage.net/search/label/SQL

Palindrome

package packageName;

public class CheckPalindromeInJava {

       public static void main(String[] args) {     

             String str = "Asdf as sa fdsa.";// if you have to check a number then comment this line

             //int num = 123909321; // if you have to check a number then uncomment this line

             //String str = Integer.toString(num); // if you have to check a number then uncomment this line

            

             str = str.replaceAll("[^a-zA-Z0-9]", "");

             System.out.println("Special Chars, question mark, full stop are removed: "+str);

            

             str= str.toLowerCase();

             System.out.println("All the characters changed to lower case : "+str);

            

             int length = str.length();

            

             String strForward = "";

             String strBackward = "";

            

             strForward = str;

             //to get the string backward

             for(int i=length-1;i>=0;i--) {

                    strBackward = strBackward.concat(Character.toString(str.charAt(i)));

             }

             System.out.println("The given string/number/phrase backward is : "+strBackward);

            

             if(strForward.equals(strBackward)) {

                    System.out.println("The given string/number/phrase is a ******* Palindrome ********");

             } else {

                    System.out.println("The given string/number/phrase is ******* NOT a Palindrome *******");

             }

       }

}

 

Output:

The characters changed to lower case : asdfassafdsa
The given string/number/phrase backward is : asdfassafdsa
The given string/number/phrase is a ******* Palindrome ********
 

 

No comments:

Post a Comment