Due: Tuesday, Nov 16, 2010 at 5pm
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
Read Chapter 6.
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.
- stdio.h provides scanf, printf, getchar
- ctype.h provides tolower, isalpha
Your program must include the following functions, each of which must be documented with pre- and post-conditions:
- 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
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
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,
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.)
- 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.
- 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 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.
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)
Enter your second phrase (30 characters or less)
>god of ruthless data!
Your phrases are anagrams
- 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 five variables: the two arrays
that hold the phrases, the two int's that hold the sizes of the two arrays,
and the int that represents the successful execution of the
If you find yourself defining more than these five variables in main(),
then the interface to one or more of your functions is incorrect.
- Use the debugger.
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
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
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