/** * Matt Kretchmar * February 13, 2006 * Palindrome.java * This program prompts the user for a series of input strings. * Each string is tested to see if it is a palindrome or not. */ import java.util.Scanner; class Palindrome { public static void main ( String [] args ) { Scanner keyboard = new Scanner(System.in); String entry; int i, size; boolean isPalindrome; // repeat testing until user enters "quit" do { System.out.println("Enter a potential palindrome (or \"quit\"): "); entry = keyboard.next(); // assume entry is a palindrome isPalindrome = true; size = entry.length(); // loop from start to halfway for ( i = 0; i < size/2; i++ ) { // if you find a character pair that don't match, // then set isPalindrome to false if ( entry.charAt(i) != entry.charAt(size-i-1) ) isPalindrome = false; } // print appropriate response if ( isPalindrome ) System.out.println("String " + entry + " is a palindrome."); else System.out.println("String " + entry + " is not a palindrome."); } while ( !entry.equals("quit") ); } }