CS 162, Spring 2004
Programming Assignment 1
Due: Wednesday, April 7

Word Scramble

Consider the following paragraph:

Aoccdrnig to rscheearch at an Elingsh uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe.

For this assignment you are to create a program (that is, an application, not an applet) that reads input from the console line by line, and scrambles the words as described above; that is, keeping the first and last letter of each word, and randomly rearranging the remainder.

The input will consist of lines of text, letters and spaces only, no punctuation. After each line is read your program should scramble the line and output the result. In addition, you should reduce all spaces to a single space (the latter requirement actually makes your program easier, not harder). Here are some sample input and output lines. Inputs are bold, output not. In addition, I've placed a vertical bar at the right of the input line just to indicate the end of line, since otherwise you can't see if there are spaces at the end. You are allowed an extra space at the end of your line (this also makes the program easier). The bar is not, of course, actually part of the input:

An interesting problem|
An isetntnireg prlebom

you see there can be spaces at the start and end |
you see three can be scepas at the sratt and end
also empty lines|
also epmty lnies
|

pretty neat|
prttey neat

Here are some additional requirements:

To solve this problem you may want to proceed in a series of smaller steps. Here are my suggestions:

Step 1. Write a program that simply reads lines of text from the input and prints them out. Advanced Topic 3.6 has some useful information. Note that the method readLine returns a null value when the end of input is encountered. You may also want to read the discussion on the loop and a half problem in Advanced Topic 6.4 (page 249).

Step 2. Modify the program above so that it reads a line of text, then identifies the starting and ending location of each word in the text. Sections 3.7 and 6.4.2 have some useful information on character processing.

Step 3. Create the final program. Information on Random numbers can be found in Section 6.5. To do the rearrangment, use the following simple approach: loop over each position you want to swap, for each position generate a random number to select another position, then swap the two characters. Since strings cannot be changed, you will want to use the class StringBuffer. Hortmann mentions this class briefly in Advanced Topic 6.3, but not enough. The following methods are useful:

String aString = "whatever";
StringBuffer sbuf = new StringBuffer(aString); // used to convert to StringBuffer
char c = sbuf.charAt(2); // used to get a character
sbuf.setCharAt(1, c)); // used to set a character
String newString = new String(sbuf); // used to convert back to string

As always, if you have trouble see the TA or the instructor.