Write a boolean method that determines if the String passed to the method is a palindrome (spelled the same backwards and forwards - e.g. tot).
Sample input:
tot everything hello abcba abba XXX
Sample output:
true false false true true
// Programmer: Andria Hunter
// Date: July 10, 2001
// Course: CSC108
//
// PalinChecker: this class is used to determine which lines of input
// are palindromes.
//
import java.io.*;
class PalinChecker {
/**
* main: This method is where the program starts execution. It
* reads input from the user and then prints out true or false
* to indicate whether each line of input is a palindrome.
*/
public static void main (String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader
(System.in));
final String STOPPER = "XXX"; // stop when this is entered
// Each iteration of this loop reads in one line of input from
// the user and calls the isPalindrome method to print out
// whether or not the line is a palindrome.
while (true) {
String line = in.readLine();
if (line.equals(STOPPER)) {
break;
}
System.out.println (isPalindrome (line));
}
}
/**
* isPalindrome: this method returns true iff 's' is a palindrome,
* i.e. it has the same sequence of letters when it is read from the
* left to the right as when it is read from the right to the left.
* Precondition: s may not be null.
*/
private static boolean isPalindrome (String s) {
for (int i=0; i<s.length()/2; i++) {
if (s.charAt(i) != s.charAt(s.length()-1-i)) {
return false;
}
}
return true;
}
}