woonie

Palindrome.java

Apr 26th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. //something wrong with this code
  2.  
  3. import java.util.*;
  4. class Palindrome {
  5.     /* use this method to check whether the string is palindrome word or not
  6.      */
  7.     public static boolean isPalindrome(String word) {
  8.         int n;
  9.         n = word.length();
  10.         Character first = new Character(word.charAt(0));
  11.         Character last = new Character(word.charAt(n-1));
  12.         boolean result;
  13.         if (first.equals(last)){
  14.             if (n == 1 || n == 2 || n == 3){   
  15.                 result = true;
  16.             } else {
  17.             result = isPalindrome(word.substring(1, n-1));
  18.             }
  19.         } else {
  20.         result = false;
  21.         }
  22.         return result;
  23.     }
  24.     public static void main(String[] args) {
  25.         // declare the necessary variables
  26.         String myWord;
  27.         // declare a Scanner object to read input
  28.         Scanner myScanner = new Scanner(System.in);
  29.         // read input and process them accordingly
  30.         myWord = myScanner.next();
  31.         // simulate the problem
  32.         boolean result = isPalindrome(myWord);
  33.         // output the result
  34.         if (result){
  35.             System.out.println("YES");
  36.         } else{
  37.             System.out.println("NO");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment