[FrontPage] [TitleIndex] [WordIndex

Note: You are looking at a static copy of the former PineWiki site, used for class notes by James Aspnes from 2003 to 2012. Many mathematical formulas are broken, and there are likely to be other bugs as well. These will most likely not be fixed. You may be able to find more up-to-date versions of some of these notes at http://www.cs.yale.edu/homes/aspnes/#classes.

1. Character counting

For this assignment, you are to write a program count.c that reads characters from stdin until EOF, and prints a count for each character that appears in the input, sorted by increasing numerical character value. Each count should be preceded by the character itself, if it is a printable non-space character, or by the character's numerical value as two hexadecimal digits surrounded by angle brackets otherwise.

For example, running the command

echo "this is a test" | ./count

should produce the output

<0a> 1
<20> 3
a 1
e 1
h 1
i 2
s 3
t 3

2. Details

  1. You may assume that each character occurs at most 232-1 times in the input. This means that character counts can fit in an unsigned int. You do not need to detect overflow.

  2. Your program should handle all 256 possible 8-bit characters. If you want to make your program portable to hypothetical architectures with a different number of characters, you may find it helpful to put #include <limits.h> at the top of your program file and use the UCHAR_MAX constant defined there (which should be 255; use UCHAR_MAX+1 to get the actual number of characters).

  3. To test whether a particular character is a printable non-space character, you should use functions from ctype.h, either isprint and isspace or the more cryptic but simpler isgraph.

  4. Since people keep asking, you should not be worried that you are missing something if you find this assignment easier than ../HW1.

3. Submitting your assignment

To submit your assignment, use the program /c/cs223/bin/submit on any machine in the Zoo, like this:

$ /c/cs223/bin/submit 2 count.c
Copying count.c
$

4. Testing your assignment

A public test script is available on the Zoo in /c/cs223/Hwk2/test.count. You can run this with no arguments in a directory containing a file count.c, and it will compile your program and run various cryptic tests on it. The tests used for grading may be different, so the score you get from test.count may not accurately predict your final grade.

If you want to see the inputs for each test and the corresponding expected outputs, look in /c/cs223/Hwk2/testfiles. Many of these files contain non-printing characters, so looking at them with cat is not recommended.

After submitting your assignment, you can run the test.count script on the submitted files with the command

/c/cs223/bin/testit 2 count

2014-06-17 11:58