copyright Steve J. Hodges   http://steveh.net/cs19/cs19-hw02.html

CS 19 Spring 2015

Assignment 2 (Word scramble)

Filename

scramble.cpp

Sample Output

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

Program Description

In this assignment, you will read an unspecified number of lines of text from STDIN. Input is terminated with EOF (ctrl-d for keyboard input). Your only output, to STDOUT, will echo the input except that the letters of any word with more than three characters will be randomly shuffled (except for the first and last letters). Use a standard shuffle algorithm as described in class. Don't scramble any punctuation, and you may assume that punctuation will only occur at the end of a word. Any word will have, at most, one punctuation character. Punctuation does not count towards the length of the word. You may also assume that each word will be separated by one space and that a line will have no leading or trailing whitespace. You may also assume that each line will have fewer than 255 characters, and no single word will be longer than 18 characters. Blank lines are allowable. You must preserve the newlines (output has the same number of lines and the same number of words on each line as when input.) (A word, here, is defined as a sequence of alphanumeric characters delimeted by whitespace.) You may use either C style strings (character arrays) or C++ style strings (string class) for this assignment, but not both. Choose whichever you have more experience working with. If you don't have a preference, then use the string class.


Sample Input

According to research at an English university, it does not matter in what order the letters in a word are, the only important thing is that the first and last letter are in the right place. The rest can be a total mess and you can still read it without a problem. This is because we do not read every letter by itself but the word as a whole and the brain figures it out anyway.


What to turn in

As usual, leave your .cpp file for this program ("scramble.cpp") in your home directory on pengo.

Sample Run

[steveh@pengo Solutions]$ ./a.out
This is a test
This is a tset
Hello, how are you today?
Hlleo, how are you toady?

Suggestions

You may find it easiest to complete this program in stages. Begin by writing a program that echos input lines of text to output. Then add the ability to break a line of text into individual words, finally add the scrambling feature.

If you use C style strings, you may find the functions strncpy(), strncmp(), strlen(), strcat(), and strtok() are useful. If you use C++ style strings, you may find the palindrome example in chapter 9 is helpful.