Programming Exercise 2


For this exercise, you will need to download the template ex2.c, and enter your code in the methods (functions) provided. You are welcome to create your own methods if you like. You will also need to download two additional files - the test program ex2test.c and these three test data files: ex2test1.txt, ex2test2.txt, ex2test3.txt. You may change or add test code to ex2test.c if you like. Both files need to be in the same directory as your ex2.c. Before you write your first line of code, you ought to try compiling the template as follows:

  gcc -o ex2 ex2test.c ex2.c -lm
This will create an executable called ex2 that you can execute as follows:
  ./ex2

Before starting on this exercise, read through the File I/O examples in Module 4, and the string example in Module 3.

In this exercise you will work with text files. Notice that the three data files are all text files, each containing words. Each line of the file will have one or more words, with one or more blanks preceding or following each word. The file will only contain the following whitespace: blanks, end-of-line (the carriage return character), and the end-of-file (EOF) character. The other characters in the file are letters or digits and will be a part of some word.

Your first task is to read a file character-by-character to identify the words in the file. The following C code reads a file character by character:

  FILE *inputFile;
  char ch;

  inputFile = fopen (fileName, "r");
  ch = fgetc (inputFile);
  while (ch != EOF) {
    /* Process character ch */
  }
  fclose (inputFile);
Use this as a template to identify beginnings and ends of words and to therefore identify words. Thus, consider a text file (such as ex2test2.txt):
blah1 blah2 blah3 blah4
nah1
nah2 nah3
The output after identifying the words should be:
Words in file ex2test2.txt:
  blah1 
  blah2 
  blah3 
  blah4
  nah1
  nah2 
  nah3
To identify these words, you will write code in the method makeWordList (char *fileName) in the template ex2.c.

Next, let's go through some of the methods in ex2.c:

Now examine the file ex2test.c. You will see that your makeWordList() is tested with the three data files you have downloaded. This constitutes the first part of your exercise.

The second part of the exercise is to compare two word lists to see if any words appear in the same order in two given files. Consider the third data file, ex2test3.txt:

blah1 blah3 blah2 blah4
nah3
nah2 nah1
nah4
Compare this with the file ex2test2.txt from earlier. You will notice that blah1 is the first word in both files, and that nah2 is the sixth word in both files. Your method compare (char *fileName1, char *fileName2) will identify such words. In particular, for the above two data files, the output should look like this:
Word# 0: same in each file
  On line 1 in file ex2test2.txt: blah1
  On line 1 in file ex2test3.txt: blah1
Word# 1:
  On line 1 in file ex2test2.txt: blah2
  On line 1 in file ex2test3.txt: blah3
Word# 2:
  On line 1 in file ex2test2.txt: blah3
  On line 1 in file ex2test3.txt: blah2
Word# 3: same in each file
  On line 1 in file ex2test2.txt: blah4
  On line 1 in file ex2test3.txt: blah4
Word# 4:
  On line 2 in file ex2test2.txt: nah1
  On line 2 in file ex2test3.txt: nah3
Word# 5: same in each file
  On line 3 in file ex2test2.txt: nah2
  On line 3 in file ex2test3.txt: nah2
Word# 6:
  On line 3 in file ex2test2.txt: nah3
  On line 3 in file ex2test3.txt: nah1
Word# 7:
  File ex2test2.txt exhausted
  On line 4 in file ex2test3.txt: nah4
To do this, simply create the word list for each file and go through in order. You will also need to identify the line number in the file that each word is found on.

Deliverables and submission: