Palindrome - } else { println("That string is...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * File: Palindrome.java * --------------------- * This file determines whether a string is a palindrome, which * is simply a string that reads the same backwards and forwards. * * Author: CS1MD3, Nov. 2008 */ import acm.program.*; public class Palindrome extends ConsoleProgram { public void run() { println("This program tests whether a string is a palindrome."); while (true) { String str = readLine("Enter a string: "); if (str.length() == 0) break; if (isPalindrome(str)) { println("That string is a palindrome.");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } else { println("That string is not a palindrome."); } } } /** * Checks whether a string is a palindrome * @param str The original string * @return The reversed string */ private boolean isPalindrome(String str) { return str.equals(reverseString(str)); } /* Returns a string with the characters reversed */ private String reverseString(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { result = str.charAt(i) + result; } return result; } }...
View Full Document

Ask a homework question - tutors are online