This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 12th, 2013  |  syntax: Java  |  size: 1.58 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.*;
  2.  
  3. public class palindrome {
  4.  
  5.         public static void main(String[] args) {
  6.                 Scanner input = new Scanner(System.in);
  7.                 System.out.println("Please enter the string you want evaluated for palindromeness: ");
  8.                 String palindromeCandidate = input.nextLine().toLowerCase();  //changes all to lowercase
  9.                 palindromeCandidate=palindromeCandidate.replaceAll(" ","");   //removes spaces
  10.                 for (int i=0; i<palindromeCandidate.length();i++){   //removes punctuation...hopefully
  11.                         if(Character.isLetter(palindromeCandidate.charAt(i))==false)
  12.                 palindromeCandidate=palindromeCandidate.replace(palindromeCandidate.substring(i,i+1), "");
  13.                 }      
  14.                 if (isPalinedrome(palindromeCandidate)) {
  15.                         System.out.println(palindromeCandidate + " is a palindrome");
  16.                 } else {
  17.                         System.out.println(palindromeCandidate + " is not a palindrome");
  18.                 }
  19.                
  20.                 //input.close();
  21.         }
  22.         public static boolean isPalinedrome(String str) {
  23.                         for (int i = 0; i < str.length(); i++) {
  24.                         char leftHand = str.charAt(i);                        
  25.                         char rightHand = str.charAt(str.length() - 1 - i);
  26.                         if (leftHand != rightHand){
  27.                                 return false;
  28.                         }
  29.                 }              
  30.                 return true;
  31.         }
  32. }
clone this paste RAW Paste Data