Lab 2: More Strings

Today, you'll continue with some more word games in order to get practice working with strings.

I. Setting up

To get started, make a directory where you want to work today (that's probably something like csci235/lab2), then change into it. You probably want to start with the (working) version of the Palindrome program that you wrote last time; so copy Palindrome.java from your account to this directory. (Do you remember how?) (If you'd rather start with a clean slate, you can copy the starting file from last week with the command

$ cp /cslab/class/csci235/labs/lab1/Palindrome.java .
Don't miss the dot.)

II. Improved Palindromes

Use emacs to look at Palindrome.java. The three non-comment lines at the beginning of the main block should now make a little bit more sense.

Based on what you know now, you might want to simplify or clean up what you wrote last time. Test it to be sure that it still works correctly.

Your program from last time may not have printed a very clear message. Fix it so that it prints the input string, in quotes, and says whether it is a palindrome, as in

'toot' is a palindrome.
'tort' is not a palindrome.

Right now, the program distinguishes based on case, so that, for example, "Pop" is not correctly recognized as a palindrome. Fix it so that it ignores case. Test it; make sure that it prints the input string as the user typed it.

This should be very simple: try to find a way to do it that does not make your loop for testing more complicated. Check out this hint if you need help. Pages 38-41 of the textbook list some of the methods available for strings. (You could look at the online documentation for class String, but that is a lot longer and contains a lot of clutter that we don't yet understand; there is also an abbreviated version of this reachaable from "Simplified documentation" on the class page.)

Now it is time to make your program ignore any spaces and punctuation in the input. Once you've done this, you should get something like

'A man, a plan, a canal: Panama!' is a palindrome.
The same hint applies here.

Note: there are methods in class Character to help you classify characters. Look for methods with names that start with 'is' and take a parameter that is of type char.

You'll notice that these methods have the word static in front of them, as does the digit() method that you used in the last lab. When a method is not static, you name it relative to an object, as when substring() is called in the second line of

String s = "1234"; 
String t = s.substring(2,3);
When you call a static method, though, you name it relative to the class, as in the use of digit() here:
int i = Character.digit('3', 10);

Make sure your modified program works, and enjoy a moment of celebration with your partner.

III. Pangrams

Your previous program tests whether a string is a palindrome. Now you'll tackle another word game.

A sentence is a pangram if it contains every letter of the alphabet at least once. Examples include

Write a program which prompts the user for a string and determines whether or not that string is a pangram. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we've seen in class) and a comment on every variable you declare.

Note: a good way to start would be to copy your palindrome program. You can do that with

$ cp Palindrome.java Pangram.java

Hint: Think about how you would do this using pencil and paper. You can do this using the methods listed in the textbook, which are also in the simplified online documentation. Need a hint? Another?

IV. Anagrams

Two words or phrases are anagrams of each other if they contain all the same letters (with the same frequency). Examples include

Write a program Anagrams.java that prompts the user for two strings. Test whether the two strings are anagrams of each other. Ignore spaces, punctuation, and capitalization. Your program should also include an opening documentation block (following the style we've seen in class) and a comment on every variable you declare.

Again, you might want to copy one of your other programs to get a starting point.

Turning it in

Make a script file that shows you compiling your programs and running them with inputs that make it clear that they work correctly. (See how to script.)

Print your three .java files and the script. (See how to print.)

Use handin to hand in the files as lab2, too. (See how to do that, too.)


Updated by Cary Gray,
original by Thomas VanDrunen
Last modified: Thu Sep 11 07:06:32 CDT 2014