CSCI 3310
Summer 2006
Small Programming Assignment 2

Assigned: May 18
Due: May 22, 11:59pm


Words

For this assignment, a word in a document is defined to be any sequence of letters that is surrounded by nonletters (or the beginning or end of the document). For example, in the document,

T'was brillig, and the slithy toves
did gyre and gimbol in the wabe;
all mimsy were the borogoves,
and the mome raths outgrabe.
"brillig" is a word, but "brill" is not. Notice that, by our definition, "T" is a word, but "T'was" is not.


The assignment

Write a program that takes two arguments from the command line, first a word then a file name. The program should report the number of occurrences of the given word in the given file, counting only words as defined above. Case should be ignored. So, for example, if the program is asked to count occurrences of "frost", then it should count "Frost" and "FROST". It should not count "frosty".


Design requirements

Design this program in the following form. Create a class of objects that count words in files. The constructor should take a word (a string or array of characters) and an open file of type istream, and the object should remember those things. The class should provide the following methods.

  1. convertToLowerCase(A) (a private class method). This class method should convert all of the letters in array A to lower case, changing the contents of array A.

  2. readword(A) (a public instance method). This should skip over leading nonletter characters, then read as many letters as possible, until it sees a nonletter. It should store the word into array A (an array of characters), as a null-terminated string. If there are no letters left, it should make A hold an empty string, by storing a null character into A[0].

  3. countWords() (a public instance method). This should successively read words (using readword), checking each to see whether it is the word that is sought, until there are no more words in the file. It should return the number of times the word occurs.

The main program should get the command line arguments, open the file and check that the file open operation was successful. Then it should create an object that counts words, and ask it how many occurrences there are of the given word in the open file. It should report the result and close the file.

Write the program in three files: wordcounter.h, wordcounter.cpp and main.cpp. File wordcounter.h should contain the class. File wordcounter.cpp should contain implementations of the constructor and methods for your class. File main.cpp should begin

#include "wordcounter.h"
and should contain the main program.


Hints

Checking for letters

If you include the cctype header file

#include <cctype>
using namespace std;
then you can use function isalpha(c) to test whether character c is a letter. (It returns a nonzero value if c is a letter, and 0 if c is not a letter.)

The command line

To use the command line, declare your main program as follows.

 int main(int argc, char** argv)
 {
   ...
 }
Then argv will be an array of strings (where each string is given as a null-terminated array of characters) and argc is the number of strings in the argv array. If your program is called count, and you invoke it using command
  count brillig Jabberwocky.txt
then argc will be 3, argv[0] will be "count", argv[1] will be "brillig" and argv[2] will be "Jabberwocky.txt".

Opening files

If you include fstream

#include <fstream>
using namespace std;
and if fname is a null-terminated string, then you can create an input file stream (type ifstream) attached to file fname as follows.
  ifstream inf(fname);
When you are done, close inf using
  inf.close();
You can ask whether the last operation failed using expression inf.fail(). Use that to tell whether opening the file succeeded, and to detect the end of the file.

Object cin has class istream (not ifstream), but ifstream is a subclass of istream. So you can use inf in a place where an istream object is expected. For example, to create a word counter that reads from inf, you might use the following.

  WordCounter wc(word, inf);
where word is the word that you want to count. To read from the standard input instead, you would write
  WordCounter wc(word, cin);


Submitting your work

Log into one of the Unix machines. Change to the directory that contains your assignment. Then give command

  ~karl/3310/bin/submit S2 wordcounter.h wordcounter.cpp main.cpp
If the only .h and .cpp files in this directory are the ones that you want to submit, you can abbreviate that to
  ~karl/3310/bin/submit S2 *.h *.cpp

You can log into the Unix machines remotely using ssh. You need to have the Ssh Secure Shell client. Log into host login.cs.ecu.edu.