Homework 3
Anagram tester
Due: Tuesday, Nov 13, 2007 at 11:59pm
Outcomes
After successfully completing this assignment, you will
be able to...
- Write a C program that uses 1-dimensional arrays
- Write functions that take arrays as parameters
- Write functions that use pointers to change a parameter's value
Before Starting
Read Chapter 6.
The Assignment
Two phrases are anagrams if you can rearrange the letters of one to
get the other. For example, an anagram of "Dormitory"
is "Dirty Room". Notice that the differences between
capital and small letters are
ignored when determining if one phrase is an anagram of another
(as are punctuation and whitespace).
Your program should ask the user to enter
two phrases, and will determine whether or not the two phrases are anagrams.
Include files
- stdio.h provides scanf, printf, getchar
- ctype.h provides tolower, isalpha
Design
Your program must include the following functions, each of which must be documented with pre- and post-conditions:
- inputPhrase This function reads in a phrase (maximum 30 characters)
that is typed at the keyboard. The function returns the phrase, stored in a
character
array. It also returns the size of the
array (the number of characters stored in the array). If the user types in more than 30
characters, only the first 30 are stored in the array.
You should define the value 30 as a global constant using the #define
directive.
Please note that you are NOT using strings in this assignment (as
described in Chapter 8 - we haven't talked about them, yet), and you should NOT be
using the string handling functions described in Chapter 8. In particular,
the
inputPhrase function should accept the user's input one character at
a time (set up a loop that reads in one character with each iteration of the
loop. The loop should terminate if the user types the ENTER key
or if 30 characters
have already been read in.)
- cleanUpPhrase The purpose of this function is to remove whitespace
and punctuation from a phrase, and to change all alphabetic characters to
lower case letters. The function reduces the size of the array as
whitespace and punctuation characters are removed. So this function will
take two parameters, both of which are changed by the function. This
function's return type should be void.
- countChar This function is given three inputs: a character (ch),
an array of type char, and the size of the array. The
function returns the number of occurrences of ch in the array. This
function is called by isAnagram.
- isAnagram Given two character arrays, and the size of each array,
the function returns 1 (true) if the two phrases stored in the arrays are
anagrams of each other, and returns 0 (false) otherwise.
The ctype library
The library ctype defines two functions that will make your job
easier. The call:
char ch1, ch2='A';
ch1 = tolower(ch2);
returns the lower-case version of ch2 ('a') to ch1. If ch2 was non-alphabetic,
tolower() would return ch2 unchanged.
The function isalpha() is a function that takes one parameter of
type char. If the character is alphabetic, the function returns
a non-zero value (true); otherwise it returns 0 (false). See pages 320-321
in the text.
Sample Execution
ANAGRAM TESTER
You will be asked to input two phrases. This program will
determine if the phrases are anagrams.
Enter your first phrase (30 characters or less)
>Douglas Hofstadter
Enter your second phrase (30 characters or less)
>god of ruthless data!
Your phrases are anagrams
Hints
- Start by drawing each function as a black box, or by writing the pre-
and post-conditions for each function. Code each function as a stub.
- Fill in the body of each function one at a time. Thoroughly test one
function before you move on to code the next one.
- The main function needs to define only four variables: the two arrays
that hold the phrases, and two int's that hold the sizes of the two arrays.
If you find yourself defining more than these four variables in main(),
then the interface to one or more of your functions is incorrect.
- Use the debugger.
Deliverables
Submit your implementation file using the following
turnin command:
/cs/bin/turnin submit cs2301 hw3 hw3.c
Programs submitted after 11:59pm on November 13 will be tagged as late,
and will be subject to the late homework
policy.
Grading
This assignment will be graded on the following areas: documentation
(including pre- and post-conditions for all functions), proper choice of
parameters and local variables, proper choice of function return types,
adherence to specifications for each function, correctness, and robustness.
Programs must compile successfully in order to receive points for
correctness
and robustness.