CS 12: Programming Assignment 3

Due Date: Friday, Oct. 27

Directory To Submit: as3

In this assignment you are going to create a program that reads in lines of text and prints them back out, except that "bad" words should be replaced with "X's".

The Functions

printLine
The printLine function should take a string (i.e. char *) as a parameter. The string contains any number of words. The function should use the strtok C++ library function to "break-up" the string into the individual words. After the function prints ">> ", each word should then be be printed out, unless it is a "bad" word, in which case a series of "X's", matching the length of the original "bad" word, should be printed instead.

The function should contain a constant array of strings to hold the list of "bad" words. See section 5.9 on page 333 of the text for an example of how to create such an array. Each word in the string, then, needs to be compared with each of the words in the list to see if it is a "bad" word. Note that capitalization should not matter (i.e if "bad" is in the list, the word "BAD" should match it), so use the strcasecmp library function. This function is exactly the same as strcmp except that it ignores case when doing comparisons.

The list of "bad" words should contain at least 5 words. While it might be tempting to you real "bad" words, please do not, as there is no guarentee that the grader will not be offended.

main
Your main program should create a character array of length 81 (use a const int for this). Use this array to read lines of text entered by the user. The user indicates s/he wants to quit entering text by hitting <RETURN> on a blank line. After each line is read in, call the printLine function to print out the words in the line.

In order to read a line of input from the user, you will need to use cin.getline instead of just cin. Also, make sure the user does not overflow the array.

mystrcasecmp
If using Visual C++, you will notice that it does not support strcasecmp. Microsoft does support an alternate function, which you can use if you wish (and figure out what it is). An alternative is to use our own strcasecmp method. Note: in order to use the following code, you need to include ctype.h in your program. (In class, I incorrectly said string.h.)

#include <ctype.h>

int mystrcasecmp( const char *s1, const char *s2 )
{
    for ( int i = 0; s1[ i ] != '\0' && s2[ i ] != '\0'; ++i )
    {
        char ch1 = toupper( s1[ i ] );
        char ch2 = toupper( s2[ i ] );

        if ( ch1 != ch2 )
        {
            return (int) (ch1 - ch2);
        }
    }

    return (int) (s1[ i ] - s2[ i ]);
}

Sample Run

Following is a sample run of the program:

This program will quote lines of text that you enter back to you, except
that "bad" words will be replaced by "X's".

Please enter as many lines of text as you like.  Hit  on a blank
line to stop.

Some people like CS12  while others do not.
>> Some people like XXXX while others do not.
It is a lot of work but hopefully not too much work!
>> It is a lot of XXXX but hopefully not too much work!
I'm sure LAB is tons of fun.
>> I'm sure XXX is tons of fun.
<RETURN>

In the sample run above, the list of "bad" words was "cs12", "work", and "lab". (Note that the second "work" on the second line was not converted to "X's". Why?)

Grading

Make sure your program is well commented and you use good style (i.e. consistent indenting, meaningful identifier names, etc. One-third of your grade is for commenting and style, so do not forget about this.