CSc 352, Fall 2002: Programming Assignment 4: Simple Pointer Programs

Out: Thu Sept 26

Due: 12:00 Noon Tue Oct 8


You are to create a number of files, as directed below, and then submit them electronically on host lectura.cs.arizona.edu using the command

turnin cs352_hw4 files

Please follow the directions below carefully: submissions that don't follow directions will be penalized heavily.

Note: As before, your programs should compile with no errors or warnings when compiled with gcc -Wall. Moreover, each program should exit with a status of 0 if execution proceeds normally, and a status of -1 if any sort of error occurs.

For this assignment, your programs should read all their inputs using the myIO library mentioned below. Furthermore, while you can declare space for buffers etc. as arrays of char, your code should process strings using pointers, not as indexed arrays.


The Problems

  1. This problem involves implementing most of the functionality of the Unix wc utility.

    Write a C program, in a file mywc.c, that reads input from stdin until end-of-file is encountered, then prints out, on stdout, the following:

    nLines nWords nChars
    where nLines is the number of lines read from the input; nWords is the number of words; and nChars is the number of characters read.

    For this problem, a word is defined as follows. Call a character ch readable if (i) ch is printable; and (ii) ch is not whitespace. I.e., ch is readable if

    isprint(ch) && ! isspace(ch)
    See the man pages for these functions for details. Then, a word w satisfies the following criteria:
    1. w consists of a non-empty sequence of readable characters; and
    2. w is separated from any adjacent word by at least one whitespace character.

    In the output generated, each adjacent pair of numbers should be separated by a single space, e.g., when printed using

    printf("%d %d %d\n", nLines, nWords, nChars);
  2. This problem involves implementing the basic functionality of the Unix sort utility.

    Write a C program, in a file mysort.c, that reads input from stdin until end-of-file is encountered, then prints out, on stdout, the input lines in sorted order. All of the input lines, including empty and blank lines, should be printed out, i.e., the number of lines printed should equal the number of lines read in.

    For sorting, use the ordering between lines given by the string library function strcmp: given two strings s1 and s2, s1 comes before (i.e., is ``less than'') s2 if

    strcmp(s1, s2) < 0.
  3. This problem involves implementing a simple spell checker.

    Write a C program, in a file myspell.c, that behaves as follows.

    1. It reads in a ``dictionary,'' i.e., a reference sequence of words, provided one per line in the file named
      /home/cs352/FALL02/hw4_stuff/WORDS
    2. It reads input from stdin until end-of-file is encountered.
    3. It prints out, on stdout, each word that was read in from stdin which does not occur in the dictionary read in from the file mentioned above.

    For this problem, a word is a sequence of letters: your program should ignore non-letter characters in the input. Your program should be case-insensitive, i.e., it should match words in the input with words in the dictionary, regardless of upper/lower case differences between them. Note that both the input (from stdin) and the dictionary (from the file specified above) can contain a mix of upper and lower case letters.

    You may assume that a word is at most 64 characters long.

    Hint: You should be able to get strcmp( ) to do all the work of checking whether an input word is in the dictionary. How would you set things up so that a call to strcmp( ) will do what you need it to do? (Don't forget the '\n' character at the end of each line of text read in: this includes the words in the dictionary file as well.)

Helper Code

A simple I/O library, myIOlib, is available to assist you in this assignment. Information about how to communicate information about include files and libraris to a C compiler is available here.