Palindrome Assignment for C Programming

advertisement
Palindrome Assignment for C Programming
M. Beeson
1
The Assignment
A simple palindrome is a character string that is equal to its reverse, for example racecar. As a
warmup for this assignment, you should write for yourself the function IsSimplePalindrome(char
*x) that determines whether x is or is not a simple palindrome. (That function will be written and
discussed in class.)
You can look up palindrome in Wikipedia, and find examples such as
Red rum, sir, is murder!
Such an example is not a simple palindrome, since to make it be the same as its reverse we
have to neglect spaces and punctuation marks. For this assignment, a palindrome is defined to be
a character string such that, if all characters other than alphabetic characters are ignored, and if
upper-case and lower-case versions of the same letter are counted as equal, it is the same as its
reverse. An alphabetic character is a character between ’A’ and ’Z’ (inclusive) or between ’a’ and
’z’ inclusive. Thus punctuation marks, digits, space, tab, carriage return, hyphens, underscores,
and other strange characters are not alphabetic characters.
The assignment is to write a function
int isPalindrome(char *x)
that returns 1 if x is a palindrome, and 0 if x is not a palindrome. You can find many examples of
palindromes in the Wikipedia article.
Note that your program should work on input strings of any length. If you write the code too
inefficiently it might not work on strings of length 100,000, for example.
2
Programming hints
Your program will involve three files: main.c, Palindrome.c, and Palindrome.h. The file Palindrome.h
contains exactly one line, the prototype of your function isPalindrome. The file Palindrome.c contains the definition of your function isPalindrome. It should have your name, the date, and
#include "Palindrome.h"
near the top. It should not contain a main function. Your file main.c is where you put your main
function. That file should also include Palindrome.h. Your main program should test Palindrome
thoroughly, in any way you wish. When it is time to submit your solution, you will submit only one
file, Palindrome.c. The system will supply the header file and its own main. This general structure
will apply to all your homework assignments in this course.
For this exercise, you may wish to make use of some functions provided by the C Standard
Library:
1
int strlen(char *x)
int isalpha(char c)
char toupper(char c)
// returns the length of a null-terminated string;
// returns 1 if c is a alphabetic character, 0 if not
// returns the upper-case version of an alphabetic character
These functions will be discussed in class, including the issue of what system header files have to be
included to use them, and how they themselves are written.
There are at least two ways of solving this problem; you are free to choose your own method.
But if you need help, here is one algorithm described in more detail: Put your left index finger
on the first character of the string, and your right index finger on the last character. Then repeat
the following, as long as your fingers haven’t met yet: if either finger indicates a non-alphabetic
character, move it to the next character (your left finger moves right, and your right finger moves
left). If your fingers indicate two alphabetic characters that are the same (or differ only in case),
then move both fingers. Otherwise this string is not a palindrome, and you can return 0 without
moving your fingers again. To implement this algorithm, use two integers, say left and right, for
your fingers, and x[left] for the character “indicated by” your left finger.
3
Testing your program
Here is a list of some examples, in a format suitable for cutting and pasting into your file main.c. I
took these examples from the Wikipedia article.
static char *examples[] =
{ "racecar",
"RACEcar",
// uppercase equals lowercase
"rotator",
"rotor",
"civic",
"Red rum, sir, is murder!",
// punctuation and spaces are neglected
"Rats live on no evil star.",
"Neil, a trap! Sid is part alien!",
"Step on no pets.",
"Dammit, I’m mad!",
"Madam, I’m Adam.",
"Madam, in Eden, I’m Adam.",
"Rise to vote, sir.",
"Never odd or even",
"If I had a hi-fi",
"Yo, banana boy!",
"Do geese see God?",
"No devil lived on.",
"Ah, Satan sees Natasha.",
"A dog, a panic in a pagoda",
"Was it a cat I saw?",
"Was it a car or a cat I saw?",
"No lemons, no melon",
"A dog, a plan, a canal, pagoda",
"A man, a plan, a canal-- Panama!"
};
2
Please make sure that your program returns 1 on all these examples before you even bother to
submit it. Also, test that it returns zero if, say, you botch up these examples by changing the fifth
character to a hyphen or something else. Then test if it works on some really long examples. Then
submit it.
3
Download