Computer Science 2611
Fall 2004
Programming Assignment 4

Date assigned: 9/27/04


Successive refinement

This program is mainly an exercise in the use of arrays, but is also an exercise in program development.

Successive refinement is a development method where you write a little bit of a program and then test it before moving on. Test your functions. You do not need to make everything work before testing. See if what you have written so far is working. Do not write the whole program before trying it..


Talking like a pirate

This assignment has you write a program to read a text document and to make some changes to it so that it sounds more like "pirate" talk. That involves making the following changes.

  1. Begin each sentence with "Argh,".

  2. Replace each occurrence of "is" and "are" by "be", and each occurrence of "Is" and "Are" by "Be"

For example, if the input is

  There is a star on the horizon.  It is
  bright.
then the output will be
  Argh, There be a star on the horizon.  Argh, It be
  bright.


Input and output

Read the input from the standard input (cin). The standard input is, by default, tied to the keyboard. But you can tie it to a file if you want to. When you run your program, use command

  a.out <document.txt
to cause the standard input to be read from file document.txt instead of from the keyboard.

Write the output to the standard output (cout). Normally, the standard output is sent to your terminal window. But you can ask for it to be written into a file. Command

  a.out <document.txt >out.txt
reads the standard input from file document.txt and writes the standard output into file out.txt.


Strings

Use an array of characters to represent a string. Just after the end of the string, store a null character '\0' to indicate the end of the string. For example, string "'abc" uses the first four bytes of the array. You store 'a', 'b', 'c' and '\0'. This is the convention that several of the library functions use for strings, and that C++ itself uses for string constants. If you write "is" in a C++ program, the compiler creates an array holding 'i', 's' and '\0'.

Note. The null character is not the same as a space. It is written '\0', not '/0' or '0'.

If you include header file <cstring>, you can use the following functions.

  1. strcpy(A,B) copies a string from array B into array A. It assumes that B ends on a null character, and it puts a null character at the end of A.

  2. strcmp(A,B) returns 0 if arrays A and B contain the same string, and returns a nonzero value otherwise. For example, expression

        strcmp(S, "is")
      
    produces result 0 if array S contains string "is" (with a null character at the end) and returns a nonzero value otherwise.


Translating strings

Write a function that translates a word into pirate talk. It should take two parameters, and input array and an output array. For example, if you call the function translate, then

  translate("is", r);
should put string "be" (with a null character at the end) into array r. If the word is not one of the words that you are translating, then copy it directly into r. For example,
  translate("happen", r);
should put string "happen" (with a null character at the end) into array r.

Try your function before moving on. Does it work?


Reading the input

Reading a word

Write a function called readWord that reads characters excluding spaces, newline characters and periods and puts them into an array. It should put a null character at the end of the array. For example, if you call this ReadWord, then

  ReadWord(A);
will read characters until it sees a space, newline or period, and store them into array A, with a null character at the end. The newline character is written '\n'. So ask whether c == '\n' to see if c is a newline character. Each line ends with the newline character.

To read a character, use

  cin.get(c);
where variable c has type char.

This function will read one extra character that it does not put into the array. At the end, put that character back into the input, so that it will be read again. Write

  if(cin) cin.putback(c);
to put c back into the input.

Reading spaces, newlines and periods

Write a function to read and echo nonword characters. It should read characters until it sees a character that is not a space, newline or period. It should print each space, newline or period that it reads. When it is done, it should put back the extra character that it read.

Make this function return a result. It should return true if it read at least one period and false if it did not return a period.


Putting the pieces together

Now write a program that performs the whole job of converting a document to pirate talk. It can repeatedly read a nonword, then read a word, then print the translated version of the word. Keep doing that until it has encountered the end of the input. You can use

  while(cin)
  {
    ...
  }
to keep going as long as there is something left to read in the standard input.

How can you print "Argh," at the start of each sentence? Do not print an extra "Argh," at the end of the output. Note that you do not need to make this part work before you test your program. See if you can get things to work without this feature, then add this feature.

You will need to create some arrays, and you will need to choose sizes for them. Rather than choosing a size where you create the array, use a constant that is easy to change. For example,

  const int MaxWordSize = 50;
defines constant MaxWordSize to be 50. Now, when you create an array to hold a word, you might write
  char word[MaxWordSize];
This is the same as
  char word[50];
with the important exception that, if you change MaxWordSize later, you do not need to change the creation of array word.


Remark

Your program will not recognize words that are adjacent to punctuation. For example, it will not recognize "is," as the word "is", and so will not translate it. That is acceptable for this program.