Docsity

CSC 230-001 HW 7, Spring 2007: Palindrome, Sort, Anagram & String Functions, Assignments of Computer Science

Toggle sidebar
bg1
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.
pf2

Partial preview of the text

Download CSC 230-001 HW 7, Spring 2007: Palindrome, Sort, Anagram & String Functions and more Assignments Computer Science in PDF only on Docsity!

Homework 7 – CSC 230-001, Spring, 2007

Issued 4/2/07, due 4/9/ Note: Same comments as made in Homework 1 apply. The answers to Q4 and Q5 should be in a single text file. Submit a single 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.

Again, comparison should be case-insensitive, and non-alphabet characters can be ignored.

  1. Repeat Q1 of HW5, but use the bitwise XOR method so as to avoid the possible problem with overflow, as we discussed in class. Briefly, this method uses the bitwise XOR operation instead of plain addition to store joint values (e.g. in the first step overwrite a with a XOR b, not a + b).
  2. Repeat Q1 of HW5, but you may call the malloc() function in the body of the function code. However, the number of ints to be rotated now has to be variable. The first argument is an int, and indicates the number of variables of type pointer to int that follow. The rest of the arguments are each of type int*.