CS 5 Fall 2009
Short Assignment #4
Due Friday, October 2

As you surely know, a palindrome is a string that reads the same forwards and backwards. Put another way, a string is a palindrome if and only if it is equal to its own reversal.

In this short assignment, you will read in a string from the console, construct its reversal, and print out the reversal of the string, as well as a message indicating whether the string is a palindrome. You will know everything you need to construct the reversal after the end of Wednesday's lecture. To print the message indicating whether the string is a palindrome, you will need if-statements, which we will cover Thursday. If you want to get a jump on this portion of the short assignment, you can read about if-statements in Section 5.2 of Lewis & Loftus.

Constructing the reversal of a string is pretty easy if you think about it the right way. You already know how to construct a string one character at a time; you did that in Short Assignment 3. To reverse a string, however, in each loop iteration, you will have to concatenate an appropriate character from the string you're trying to reverse, rather than the fixed character '#'. And you'll have to think carefully about how you're going to concatenate it.

You will almost certainly want to start with the reversal as an empty string. By an "empty string" we mean a String object that exists, but the string that it holds has zero characters. The string literal for an empty string is "", that is, two consecutive double-quotes with nothing between them (not even a space character). Note that the following two lines of code are really different:

String reversal = null; and String reversal = ""; In the first line, reversal refers to no String object at all. If you tried to call a method using reversal, say reversal.toUpperCase(), you would get the dreaded "null pointer exception" and your program would crash. In the second line, reversal refers to a String object that has no characters. Here, you can call a method using reversal. For example, the result of calling reversal.toUpperCase() would also be an empty string.

Remember that to determine whether two strings are equal, we use the equals method. For example, if s1 and s2 each references a String object, then the call s1.equals(s2) returns a boolean indicating whether the strings have exactly same characters in the same order. Since equality is symmetric, the call s2.equals(s1) would return exactly the same result. (I remind you about the equals method in case you have seen Java's == operator; do not use this operator to determine whether two String objects have the same contents. We will see soon enough what the == operator does when you give it strings.)

A few hints:

Here are some sample runs from my version of the program:

Enter a string: radar The reversed string is radar radar is a palindrome Enter a string: banana The reversed string is ananab banana is not a palindrome Enter a string: t The reversed string is t t is a palindrome Enter a string: The reversed string is is a palindrome The input to the last run is an empty string, which is a palindrome since, by default, it equals its own reversal.

Hand in your code, along with at least four runs. One run should be on a string that is a palindrome with at least five characters, one run should be on a string that is not a palindrome, one run should be on a string with exactly one character, and one should be on an empty string. If you write your code simply and elegantly, it will just work correctly on an empty string with no additional effort required on your part.


Back to Short Assignments
Thomas H. Cormen <thc@cs.dartmouth.edu>
Last modified: Tue Sep 29 11:33:56 2009