COSC 060 Introduction to Software Problem Solving

Fall 2008

Homework Assignment #2

"We are the keepers of the sacred words: Ni, Ping, and Nee-womm."
"Those who hear them seldom live to tell the tale!"
-- Monty Python and the Holy Grail
Due: Wednesday, September 10th, 11:59PM CDT
Submit: Turn in your "Project2.java" using the turnin command on Morbius, as instructed in Lab.
Work is to be completed individually. You may submit multiple times, but only the last turnin will be kept. The automatic submission system will not accept work after the deadline.

Word Filter

The rise of the Information Age has rekindled many debates on the topic of censorship. Regardless of ideological stances, the general problem of detecting and censoring content remains surprisingly complex.

Write a program that prompts for a "forbidden" word, prompts for a replacement word, and then filters a body of text. The forbidden word should be replaced whenever it occurs, regardless of uppercase or lowercase. End with a printout counting the number of replacements performed.

Example Runs

In the examples below, I use text in blue to distinguish the output of the program from the input I typed. This is for purpose of clarity only; your program will not print text in different colors.

Example #1

Enter word to be filtered out:
ni
Enter word to replace it with:
herring
Enter text to filter. Hit Control-D to stop.
The wet dog ni
sighs by the dry ni fire
ni at night.
The
wet
dog
herring
sighs
by
the
dry
herring
fire
herring
at
night.

Replaced "ni" with "herring" 3 times.

Example #2

Enter word to be filtered out:
foo
Enter word to replace it with:
bar
Enter text to filter. Hit Control-D to stop.
FOO fOo Foo Oof OOF oOf and so forth.
bar
bar
bar
Oof
OOF
oOf
and
so
forth.

Replaced "foo" with "bar" 3 times.

Notes

  • Note that the output does not need to preserve the original lines or spacing of the input.
  • Depending on how you test your program, you may see lines of output interleaved with input as you type your tests in. This is normal.
  • Note that in the example above, "night" does not become "herringght"; you are not required to filter out the word if it is part of a larger word.
  • In order to read in an arbitrary number of words, you will need to use a simple loop structure. Instantiate a Scanner object:
    Scanner keyboard = new Scanner(System.in);
    and then form a loop that will continue reading words until the "end of input" marker is reached:
    while (keyboard.hasNext())
    {
    ...
    }

    The code between the curly braces will be repeated until the user pressed the Control key and the D key at the same time. (Note: Running on Windows, a user must hit Control-Z, instead.)
  • We're starting to use objects in the Java Library more thoroughly now. Documentation for the Scanner and String classes can be found in your textbook, and also online in the Java API documents: Scanner class and String class.

  • Back
    [Revised 2008 Sep 05 11:45 DWB]