The demonstrations appearing in this section implement the algorithms developed in the previous section and rely on previously introduced concepts. Please review the following as needed:
itoa, short for integer to ASCII, converts an integer to a C-string - ASCII is used here as a synonym for text or string. The ANSI standard does not require the itoa function, implying that some compilers don't support it and that it goes by various names. Figure 3 implements a simple itoa version.
_itoa(int value, char* s, int base): a common version. The arguments are (l to r): the number to convert, the C-string to hold the converted number, and the radix or base (i.e., base-10) of the converted number.
_itoa_s(int value, char* s, int size, int base): Microsoft's safe or secure version. The third parameter is the size of the C-string array or buffer.
cpalnumber.cpp presents two C-string-based programs that solve the Palindrome-Number Problem. Both programs follow the solution outline formed in that section, and each program implements one of the algorithms developed there. The first program (Figure 1) uses a single C-string and compares pairs of characters in it. The second program (Figure 3) copies the C-string that contains a potential palindrome, reverses it, and then compares the original and the reversed C-strings for equality.
Programming The "Finger" Solution
Converting Numbers To C-Strings
So, what happens if you use a compiler that doesn't support any version of itoa? You can find its source code on the internet, but we can also write a simple version. Given an integer, our task is to convert that integer into a string that looks like the number. We must convert each digit of the number into the ASCII character that represents that digit and then sequence the characters together to form a string.
The solution involves working with an integer one digit of the number at a time. If we work from left to right, the resulting string will contain the digits in the correct order. However, working from left to right is more difficult because we don't know how many digits are in the number, making it difficult to isolate them. Working from right to left is easier, but doing so converts the digits to characters in the reverse order. So, we'll use a stack to reverse the digits. (Download links to the stack code used here are found at the bottom of the Automatic Stack Implementation page.)
Before we can use our itoa function, we must modify the cpalnumber.cpp code in Figure 1 in three ways:
Include the stack header file: #include "stack.h"
Add a prototype at the top of the file: void itoa(int n, char* s);
Replace the function call _itoa_s(square, s, 100, 10); with a call to our function: itoa(square, s);
The Reverse Solution
A palindrome is a string that reads the same forwards and backward. The program described below relies on that definition to solve the palindrome-number problem. The solution copies the candidate string, reverses it, and then compares it to the original string. If the two are the same (i.e. if they are equal), then the candidate string is a palindrome; if the two are not the same, then the candidate is not a palindrome.
This version of the program leaves in place the itoa function written above, but you may switch back to _itoa_s if you wish. The reverse solution (Figure 3) has a great deal in common with the "finger" solution (Figure 1), so only the changed code is described. The code for reverse function is presented in Figure 4.
Caution
if (r == s) will compile but does NOT test the contents of the two C-strings for equality. Both r and s are character arrays, and (like all arrays) the array names are addresses, so r == s returns true if and only if r and s refer to the same C-string. So, r == s will always be false in this program!
The reverse Function
You should carefully compare the body of the reverse function with the "finger" loop in the first version of the program. Although the two versions of the program seem to approach testing for a palindrome in quite different ways, their code is quite similar! The key to understanding this function is to notice that the loop only goes to the middle of the string. What would happen if we allowed the loop to go to the end of the string?
C-string s is a null-terminated character array, but, like all arrays, the program passes it by-pointer. Pass by pointer is an INOUT passing mechanism (see Data Flow Summary)
The for-loop operates for half the length of string s, swapping pairs of characters
Swapping array elements requires a temporary variable. The swap operation saves the leftmost character, s[i], in temp, "freeing" or making available the location s[i]
The operation copies the rightmost character, s[strlen(s) - 1 - i], into the now "free" location of the leftmost character, s[i] - i.e., it overwrites the character currently saved in s[i] - "freeing" the array element at location s[strlen(s) - 1 - i]
Next, the swap operation copies the character saved in temp to the location of the rightmost character, s[strlen(s) - 1 - i]
General Palindromes
"A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward...Sentence-length palindromes ignore capitalization, punctuation, and word boundaries." Our previous palindrome detection algorithms are not robust enough to detect these general palindromes. Fortunately, we only need to filter out the spaces and punction characters and convert all the alphabetic characters to the same case. Once these steps are complete, either of our palindrome algorithms will work.
Downloadable Code
All source code files are formatted with 8-character tab stops.