Scrambling a String - Assignment


The Problem

Manually swapping characters works fine for a short string, but not for an entire paragraph.

One way to scramble a whole paragraph is to reuse the scramble map you determine on a short string. Thus, the short string is used as a prototype for how to scramble a whole paragraph. For example, consider a scramble map on the string "example":

You can reuse this map as a sort of sliding window that travels over a paragraph. Since the prototype string that was scrambled was of length 7, this will be a sliding window of length 7. For example, consider the following paragraph:

The state of the union is like
that conglomerate of stars that
shine at night.

To use the map as a sliding window, start by applying it to the first 7 characters of the paragraph. This will map "The sta" to " Tehtsa".

Next, advance the window to the next 7 characters in the paragraph. This maps "te of t" to "ot e ft".

The only tricky parts are what to do when we get to the end of a line or the end of the paragraph.

For example, after scrambling "The sta", "te of t", "he unio" and "n is li", we are left with "ke" at the end of the first line.

The way we'll choose to handle that is for the window to wrap around to the next line (and include the end of line marker). In this example, "tk\neaht" will be produced (i.e., "tk" on the first line and "eaht" on the second line).

Finally, when the end of the paragraph is reached, there might be fewer than 7 characters left. We'll handle this by padding the rest of the positions with spaces.


The Program

For this assignment, you will begin by completing the basic program discussed in lab.

To complete the assignment, change the program so that it works as follows:

% scramble
Enter a prototype string to scramble (enter no spaces and no more
than 80 characters).

String? example

Now enter pairs of numbers representing positions to swap
to create the scrambled version (e.g., "1 3").  Positions are
numbered between 1 and 7 for this string.  Enter '0 0' when
you are done.

Swap? 5 6
New scrambled version: examlpe

Swap? 4 2
New scrambled version: emaxlpe

Swap? 1 2
New scrambled version: meaxlpe

Swap? 0 0

Now enter the paragraph to be scrambled (enter a blank line
when you are done):

The state of the union is like
that conglomerate of stars that
shine at night.
<blank line>

Scrambled paragraph is:

 Tehtsaot e ftuh einosni l itk
eahtn oclgoamreet so fatrhst ta
nsih eaitn hgt . 

Note that you must read in the whole paragraph before outputting the scrambled version.


Your Task

You must add code to the lab exercise that allows the paragraph to be entered and printed out in scrambled order.


BU CAS CS - Scrambling a String - Assignment
Copyright © 1993-2000 by Robert I. Pitts <rip@bu.edu> All Rights Reserved.