YCP Logo Lab 15: Word count

Getting started

If you have a solution open in Visual Studio, close it (File->Close Solution).

Download CS200_Lab15.zip. Import it into Visual Studio (File->Import...).

Add your code to the source file called WordCount.c.

Your task

Write a program to count the number of words that the user types. For the purposes of this lab, a word is a sequence of non-whitespace characters. As in Lab 14, the user will type Control-Z followed by Enter to signal the end of input.

Example run (user input in bold):

It is a truth universally acknowledged, that a single man in
possession of a good fortune, must be in want of a wife.
^Z
The input contained 23 words

Hints

The program should contain a loop that looks like this:

while (!feof(stdin)) {
    int ch;

    ch = getchar();

    if (ch >= 0) {
        /* do something with the character value read */
    }
}

If the last character value read is stored in the variable ch, the following condition returns true if ch contains a whitespace character:

isspace(ch)

A good approach to this problem is to use two variables: one to keep track of the number of words seen so far, and another to keep track of whether the last character read was part of a word or was a space. After reading each character, determine whether or not the character was a whitespace character, and update the variables appropriately.

Submit

When you are done, choose Tools->Submit to submit your completed lab to the server. You will need to enter your Marmoset username and password.

If the Tools->Submit option does not work, create a zip file containing the files in your project, and upload it to the submission server as lab15. The server URL is

https://camel.ycp.edu:8443/