my code stock.com

Snippet options

Download: Download snippet as palindrome-in-java.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package palindrome;

     public class Palindrome
     {
               public static boolean isPalindrome(String stringToTest)
               {
                    String workingCopy = removeJunk(stringToTest);
                    String reversedCopy = reverse(workingCopy);
                    return reversedCopy.equalsIgnoreCase(workingCopy);
               }
             
               protected static String removeJunk (String string)
               {
                    int i, len = string.length();
                    StringBuffer dest = new StringBuffer (len);
                    char c;
                    for (i=(len-1); i>=0; i --) 
                    {
                         c = string.charAt(i);
                         if (Character.isLetterOrDigit(c))
                         {
                              dest.append(c);
                         }
                    }
               return dest.toString();
               }

               protected static String reverse (String string)
               {
                    StringBuffer sb = new StringBuffer(string);
                    return sb.reverse().toString();
               }

               public static void main(String[] args) 
               {
                    String string = "Madam, I'm Adam.";
                    System.out.println();
                    System.out.println("Testing whether the following" + "striing is a palindrome");
                    System.out.println(" " + string);
                    System.out.println();
        
                    if(isPalindrome(string))
                    {
                         System.out.println("it is a palindrome!");
                    } else 
                    {
                         System.out.println("it is not a plaindrome!");
                    }

                    System.out.println();
               }
     }

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.