Homework 3
Anagram tester

Due: Tuesday, Nov 16, 2010 at 5pm


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


Your program must include the following functions, each of which must be documented with pre- and post-conditions:
  1. inputPhrase (Please note: We will discuss on Friday how to write functions like inputPhrase that return more than one value. In the meantime, the beauty of the design methodology we've been using means that you don't need to wait until Friday to begin working on this assignment. Implement inputPhrase as a stub and work on other pieces of the program.) This function reads in a phrase (maximum MAXSIZE characters) that is typed at the keyboard. The function stores the characters of the phrase in a character array. If the user tries to enter a phrase with more than MAXSIZE characters, the function returns 0 to indicate failure and the contents of the array is considered invalid. If the user enters MAXSIZE characters or less, the function returns 1 to indicate success (the data in the array is considered valid), and the function also returns the size of the array (the number of characters stored in the array).

    You should define MAXSIZE 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 strings 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 MAXSIZE characters have already been read in.)

  2. cleanUpPhrase The purpose of this function is to remove whitespace and punctuation from a phrase (that's stored in a character array), 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. The function returns the new size of the array.

  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 sizes 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 312 - 315 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)
>This phrase is so long that I'm sure I'm going to get an error message.
Phrase too long.  You are restricted to 30 characters.

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



The README file that you submit with each assignment should serve as a user's guide - it is a piece of "external" documentation that the user of your program can refer to for information about how to use the piece of software you're providing. As such, the README file should contain a description of what the program does, what the user interaction will look like, and what assumptions and/or restrictions there are that might affect the way the user interacts with the program. It is not enough to say, "The assumptions and restrictions are listed in the assignment." Spell them out in the user guide. When you purchase a piece of commercial software you get a user guide that tells you everything you need to know to be able to use the software. So it is here. It's OK if you cut and paste from the assignment description, if that's what you need to say in the README file, but it does need to be included. Finally, as a user guide, the README file should include instructions to the user on how to compile and run the program.

We will be looking specifically for these elements (description, instructions on how to use the program, assumptions and restrictions, instructions on how to build the program) when grading your README files.

From your Linux account, submit your C source code file and your README file using the following turnin command:

/cs/bin/turnin submit cs2301 PROJECT3 anagrams.c README

Programs submitted after 5pm on November 16 will be tagged as late, and will be subject to the late homework policy.


Here is the grade sheet that the TAs will use when they grade your programs. Programs must compile successfully in order to receive points for correctness.