Voluntary C Exercises


These assignment exercises are intended to get you familiar with the C programming language. While they are optional, it is important that you attempt them to make sure you are comfortable programming in C. This is to help insure that you are able to attempt the main assignments for the subject.

C Language References

For help with the C language please see the documentation links in the left margin or the C books in the reference list.

You may also find the C language components of the following courses useful as well:

COMP1021 Computing 1B

COMP2041 Software Construction

Exercise 1: Spell Checker

In this exercise, you will implement a primitive spell checker. The user types English words on standard input, and your program should print yes if the spelling is correct, and no if not.

The spell checker should keep reading from standard input until end­of­file. In Unix, an end­of­file can be sent to a process reading from the terminal by hitting Ctrl+D.

Here is an example session with your spell checker:

% ./spellcheck
Spell checker ready.  Type a word now.

You should use the English dictionary contained in the file words. Your program should initialize itself by reading this file and inserting all the ./words into a database.

You must implement your spell checker database with a hash table. Skeleton code, with an example hash function, is in the file spellcheck.c. You are free to modify this code if you wish.

All memory allocated with malloc() MUST be freed with free().

Exercise 2: Alphabetical Sort

In this exercise, you will sort a file into alphabetical order using a binary tree.

The file ./words is already alphabetically sorted, so you should use the file random.words instead. This file contains the same words as ./words file, but in random order.

Your program should read the file to be sorted from standard input and write the sorted file to standard output. For example, you might run your program with the following command:

% ./mysort <random.words >sorted.words

You must insert all these words into a binary tree, and then print them out by performing an in­order traversal of the tree. Compare your result with the original ./words.

Skeleton code is in the file mysort.c.

All memory allocated with malloc() MUST be freed with free().

Hint: a post­order traversal algorithm is needed when freeing the nodes.