CSE 413 Autumn 2006

Assignment 4 -- C Warmup

Due: Electronic turnin due no later than 11:00 pm, Thursday, Nov. 2. Printout showing test cases due in class Friday, Nov. 3.

The purpose of this assignment is to gain experience with basic C programming. The algorithms involved are fairly simple; the point is to become familiar with the langauge, some basic library functions, and the Unix programming environment.

1. Word Find

Write a program named wordfind that reads a file from standard input and prints on standard output each of the lines from the standard input that contains a string given on the wordfind command. For example, suppose we have the following a file named silly.txt containing the following:

   How now
   brown cow?
   
   The quick sly fox jumped over
   the lazy brown cow.

If we compile the program and call it wordfind, then enter the command wordfind cow <silly.txt, the following should be written to standard out:

   brown cow?
   the lazy brown cow.

This is the same output that the Unix program grep would produce searching for a word in a file; the difference between wordfind and grep is that wordfind searches only for exact matches of the given string, while grep can search for arbitrary regular expressions.

Your program should complain if no argument is given on the command line and return with a non-zero exit status in that case. (i.e., return a non-zero value from main if no string is supplied).

2. Word Count

Write a program named wordcount that reads from standard input and writes to standard output the number of lines, words, and characters that appear in the input. For example, command wordcount <silly.txt (using the silly.txt example file above), should print something like

  5 14 70

More specifically, the output from wordcount should match the output from the standard unix command wc on the same file.

Details

Hints

What to Hand In

Turn in a copy of your C source files using this online turnin form.

Hand in the following:

  1. A printed copy of your C code.
  2. A printout of a Unix session showing some sample input and the output of your programs given that input.