// package chapter7; /** * Title: Chapter 7, "Strings" * Description: Examples for Chapter 7 * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // CheckPalindrome.java: Check whether a string is a palindrome // import chapter2.MyInput; public class CheckPalindrome { /**Main method*/ public static void main(String[] args) { // Prompt the user to enter a string System.out.print("Enter a string: "); String s = MyInput.readString(); if (isPalindrome(s)) { System.out.println(s + " is a palindrome"); } else { System.out.println(s + " is not a palindrome"); } } /**Check if a string is a palindrome*/ public static boolean isPalindrome(String s) { // The index of the first character in the string int low = 0; // The index of the last character in the string int high = s.length() - 1; while (low < high) { if (s.charAt(low) != s.charAt(high)) return false; // Not a palindrome low++; high--; } return true; // The string is a palindrome } }