Homework 3
Anagram tester

Due: Tuesday, Nov 13, 2007 at 11:59pm

Outcomes

After successfully completing this assignment, you will be able to...

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

Design

Your program must include the following functions, each of which must be documented with pre- and post-conditions:
  1. 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.)

  2. 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.

  3. 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.

  4. 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


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.