Homework 7 – CSC 230-001, Spring, 2007
Issued 4/2/07, due 4/9/07
Note: Same comments as made in Homework 1 app ly. The answers to Q4 and Q5 should be in a s ingle text
file. Submit a sing le source file for Q1, Q2 and Q3 ; the file should be named according to the conventions.
1. Write a function is_palindrome that accepts a single argument of type
const char * and returns a single int , the value of which is 1 if the string
pointed to by the argument is a palindrome, 0 otherwise. (A palindrome is a
string that reads the same when read forward and reverse; “madam” is a
palindrome, but “adam” is not.)
Specifically state any assumptions you make about the length of the string, or any
other assumption. Include as much error checking as possible so that violations of
such assumptions are treated gracefully (e.g. program should print out error
instead of crashing – extend the set of return values of the function if you want
to).
Try to write a version in which only lower and upper case letters and numerical
digits are considered in testing the palindrome nature – blank and punctuation is
ignored. It should also be case-insensitive. Thus the string “I, madam, I
made radio! So I dared! Am I mad? Am I?” would successfully
test as a palindrome.
Do not use any string library functions.
2. Write a function sort_file that opens a text file called input.txt which
contains several strings, and prints out the same strings, but sorted in alphabetical
order. Each string may be assumed to contain only lower and uppercase letters,
and the sorting should be case-insensitive, but the strings should be printed out in
original case, and if strings contain non-alphabetical characters, the program
should fail gracefully, rather than crash.
Specifically state any assumptions you make about the length of the string, or any
other assumption. Include as much error checking as possible so that violations of
such assumptions are treated gracefully (e.g. program should print out error
instead of crashing).
3. Write a function is_anagram that accepts two arguments of type const
char * and returns a single int , the value of which is 1 if the strings pointed
to by the arguments are anagrams of each other, 0 otherwise. (A string is an
anagram of another if its letters can be re-arranged, no repetition, no leaving out,
into the other. “O, Draconian devil!” is an anagram of “Leonardo da
Vinci”. “Oh, lame saint!” is an anagram of “The Mona Lisa”.
And “Mother-in-law” and “Woman Hitler” are anagrams of each other.