Advertisement
brilliant_moves

Palindrome1.java

Feb 8th, 2015
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.94 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Palindrome1 {
  4.  
  5.     /**
  6.     *   Program: Palindrome1.java
  7.     *   Purpose: Palindrome tester, version 1
  8.     *   Creator: Chris Clarke
  9.     *   Created: 08.02.2015
  10.     */
  11.  
  12.     public static void main (String [] args) {
  13.  
  14.         Scanner keyboard = new Scanner (System.in);
  15.         System.out.println("Palindrome Program version 1.0");
  16.         System.out.print("Enter a word: ");
  17.         String word = keyboard.nextLine().toLowerCase();
  18.  
  19.         boolean isPalindrome = true;
  20.         int left = 0;
  21.         int right = word.length()-1;
  22.  
  23.         while (left<right) {
  24.             // compare first and last characters
  25.             if (word.charAt(left) != word.charAt(right)) {
  26.                 isPalindrome = false;
  27.                 break; // out of while loop
  28.             } // if
  29.             left++;
  30.             right--;
  31.         } // end while
  32.  
  33.         if (isPalindrome) {
  34.             System.out.println("The word is a palindrome!");
  35.         } else {
  36.             System.out.println("The word is *not* a palindrome!");
  37.         } // end if
  38.  
  39.     } // end main()
  40.  
  41. } // end class Palindrome1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement