Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Palindrome1 {
- /**
- * Program: Palindrome1.java
- * Purpose: Palindrome tester, version 1
- * Creator: Chris Clarke
- * Created: 08.02.2015
- */
- public static void main (String [] args) {
- Scanner keyboard = new Scanner (System.in);
- System.out.println("Palindrome Program version 1.0");
- System.out.print("Enter a word: ");
- String word = keyboard.nextLine().toLowerCase();
- boolean isPalindrome = true;
- int left = 0;
- int right = word.length()-1;
- while (left<right) {
- // compare first and last characters
- if (word.charAt(left) != word.charAt(right)) {
- isPalindrome = false;
- break; // out of while loop
- } // if
- left++;
- right--;
- } // end while
- if (isPalindrome) {
- System.out.println("The word is a palindrome!");
- } else {
- System.out.println("The word is *not* a palindrome!");
- } // end if
- } // end main()
- } // end class Palindrome1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement