

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Page 1 of 2
A palindrome is a string of letters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For example:
A man, a plan, a canal: Panama!
Write two (2) C++ programs to determine if a string represents a palindrome. The first program is based on string objects while the second is based on cstrings.
Program #1 (named palindrome.cpp)
Suggestions
It is probably easiest to first remove all of the spaces from the string and to then do a character-by- character comparison in a for-loop.
The following code fragment (which you may use in your program) will remove the spaces from a string object (see p. 307):
for (int i = 0; i < s.length(); i++) if (s[i] == ' ') // single space s.erase(i, 1);
See palnumber (written in class) for an example of doing a character-by-character comparison of string objects.
Although much of the code for this assignment is provided for you, you should understand what the code is doing (who knows what could be on the next mid-term :-)
Page 2 of 2
Program #2 (named cpalindrome.cpp)
Suggestions
Each word entered on the command line is a space-separated string of characters, which the system provides to your program as a cstring located in the argv array. (Note that the following is not code that you write but is an illustration that corresponds to the input illustrated in step 1 above.) In this example, argc = 8 argv[0] = cpalindrome (the program name – often not of interest) argv[1] = a argv[2] = man argv[3] = a argv[4] = plan argv[5] = a argv[6] = canal argv[7] = panama
The test input will not contain punctuation marks or mixed case letters
Concatenate the command line arguments into a single cstring, which can then be checked as above char palindrome[1000] = “”; // empty string for (int i = 1; i < argc; i++) strcat(palindrome, argv[i]);
Upload two files to WSU Online: palindrome.cpp and cpalindrome.cpp (please do not zip the files).
The test bed program is provided with the assignment.