Word Count
In this activity,
you will make a program
called wordCount.c
.
This program should read in characters
using getchar()
until the end of input is reached.
Your program should count the number of words it reads. For simplicity, characters that are part of a word are
- lowercase and uppercase letters,
- numbers,
- the apostrophe,
- the hyphen (
-
), and - underscore (
_
). Every other character is considered a separator between words.
Remember, you may not always start or end with a word and multiple word separators doesn’t indicate multiple words.
For example:
$ ./wordCount The quick brown fox jumped over the lazy dog. Ctrl+D 9 words
$ ./wordCount only three words Ctrl+D 3 words
Remember,
getchar()
will read in the next character from input
and return it.
When it reaches the end of input,
it will return the value EOF
.
You can tell a program
to stop reading characters
by using Ctrl+D
which will cause getchar()
to return the EOF
value.
To read from a file
instead of typing in text,
you can use <
.
For example:
$ ./frequencyAnalysis < myTestFile.txt
To tell the program you have finished typing, you can press Ctrl+D.
To run some simple automated tests:
$ 1511 autotest wordCount
To run Styl-o-matic:
$ 1511 stylomatic wordCount.c Looks good!
You’ll get advice if you need to make changes to your code.
Submit your work with the give command, like so:
$ give cs1511 wk05_wordCount
Or, if you are working from home, upload the relevant file(s) to the wk05_wordCount activity on Give Online.