This page last updated on: Jan. 7, 2002
Jan. 28, 2002 by end of day
Write a program which tests and counts whether one or more character strings are palindromes. A palindrome is a sentence (or word) which reads the same forwards and backwards. For example, the following two words are palindromes: rotor, toot. Most definitions of a palindrome do not count spaces or special characters as significant, thus the following sentence is a palindrome: "A man, a plan, a canal, Panama!".
This exercise has some basic C++ functionality (simple input/output and class design), but is also intended to evaluate C design and coding techniques (functions, pointers, for example), and multiple source file development.
The strings to be tested are entered through the command line. For example:
C:\palcheck xyz rotor "A man, a plan, a canal, Panama!" 'xyz' is not a palindrome 'rotor' is a palindrome '"A man, a plan, a canal, Panama!"' is a palindrome The total count of palindromes is 2
Note that double quotes can be used in DOS / Windows for sentences. If no command line arguments are entered, an appropriate usage suggestion or error message should be displayed.
Design and implement a simple 'counter' class that counts how many palindrome strings have been supplied to it. Provide three public member functions - a default constructor, a member function that determines whether a string is a palindrome or not (for example isPalindrome), and a member function to return the number of palindrome strings that have been presented to it (for example getPalindromeCount).
Implement the character string parsing using pointers (not indexes). Within the isPalindrome member function, ignore non-alphabetic characters by using the appropriate standard C library character function. In addition, make the function case insensitive ( 'A' will match 'a') without modifying the original string, also by using the appropriate standard C library character function.
In the main function, use C++ iostreams for the output display:
#include <iostream> // standard C++ I/O header file std::cout << "Some output" << std::endl;
The iostream class and the cin and cout objects will be explained in more detail as the class progresses.
The isPalindrome member function should take a const char * parameter and return a bool as the return type. The return value will be true or false.
Use an initializer list to initialize the internal counter (rather than an assignment statement in the constructor).
Separate the class source code from the main function source code. The class declaration will be in a header file and the class implementation code will be in an implementation file. There will be three files, named pal.cpp, pal.h, and palmain.cpp. The header file should be included in pal.cpp and palmain.cpp. (Note that the preferred extension for C++ source files differs between operating systems and C++ compiler environments. Other extensions include .C, .cc, and .cxx. Any of these extensions are acceptable, although .cpp is preferred.)
An empty string (or null pointer passed in as a string) is not a palindrome, and a one character string is a palindrome (by definition). Strings composed of all non-alphabetic characters or spaces are not considered a palindrome for this assignment. The isPalindrome member function should handle all cases with appropriate checking.
Direct pointer arithmetic will be used:
char const * leftPtr; // this line could also contain initialization
char const * rightPtr; // this line could also contain initialization
// ...
while (leftPtr <= rightPtr) {
if (*leftPtr // ...
// ...
++leftPtr;
}
Two pointers are needed, one moving inwards from the beginning of the string, and one moving inwards from the end of the string. When two characters do not match, the string is not a palindrome. When the pointers cross, the check is completed and the string is a palindrome. Look in the standard C library for two character utility functions, one that checks if a character is alphabetic, and one that converts to upper (or lower) case.
Note that a special check for strings composed of all non-alpha characters must be performed.
The initializer list is sometimes called a constructor initializer list, or member initializer list.
C and C++ features used in this assignment include command line argument handling, function calls, pointer handling, character strings, source code management, I/O streams, simple C++ class design, and standard C string and character functions.