Due: Electronically: 8am Friday, October 26, 2007. On paper: Printout showing test cases due at the beginning of class on Friday, October 26, 2007.
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.
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).
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.
'\0'
at the end of each input line).
However, it should be easy to change this limit by changing a single number
in a #define
directive and recompiling the program.fgets
or
something similar to read each line into a string in a single operation.
Don't read the input one character at a time.stdio.h, string.h,
and
ctype.h
may be useful.
Refer to Section 7 in the
Essential C guide posted on our C references page. Be sure
to #include
any libraries you use at the top of your program.wordcount
program, "words" are defined
as sequences of characters separated by whitespace (blanks, tabs, newlines,
etc.). Specifically,
a character is defined as whitespace if the C standard library function
isspace
returns true for that character.wordfind
program.
Incremental development and testing will definitely save you time in the
long run,
even if it involves writing some extra code
to test things.gdb
can be very useful for isolating problems, but never
underestimate the usefulness of strategically placed printf
statements
for debugging.wordfind
command
line, are supplied to the main program in a pair or parameters. The full
specification of main is int main(int argc, char *argv[])
.
Parameter argc
is the number parameters on the command line.
The first array element argv[0]
is, by convention, the program
name (wordfind
), so the number of arguments will always be
at least 1.Turn in a copy of your C source files using this link.
Hand in the following: