What is a palindrome? A palindrome is a string that is the same when read left to right as when read right to left. Let's write a program that determines if an arbitrary string is a palindrome. Assume that the string can have blanks, punctuation, capital letters and lower case. Palindrome Examples: A man, a plan, a canal, Panama. Able was I 'ere I saw Elba. Madam, I'm Adam. Racecar Sample Program Output: Enter your palindrome or type quit: A man, a plan, a canal, Panama. Enter your palindrome or type quit: Be at a time I emit a beat Enter your palindrome or type quit: Racecar Enter your palindrome or type quit: A lad named E. Mandala Enter your palindrome or type quit: A Toyota's a Toyota Enter your palindrome or type quit: Race and tell a dancer Enter your palindrome or type quit: quit Palindromes: A man, a plan, a canal, Panama. Racecar A lad named E. Mandala A Toyota's a Toyota NOT Palindromes: Be at a time I emit a beat Race and tell a dancer * The space is created using a tab character. Palindrome Algorithm 1. Make sure to keep the original string. 2. Remove all punctuation, whitespace and special characters. 3. Convert the string to lower case. 4. Create a reverse copy of the string. 5. Compare the clean version to the reverse version. If the clean string equals the reverse, it is a palindrome Otherwise it is not… Make sure not to change the original string! Program Functions This prgram requires 6 functions: boolean isPalindrome(string palindrome); // isPalindrome passed the input string and calls all of the other functions to prepare the string. // Once the string is cleaned, it tests if it's a palindrome. // @param string the string to check. // @returns true if it's a palindrome and false if not. string removePunctuation(string sentence); // Create a copy of the string and remove punctuation from the copy leaving the original alone. // Use the cctype functions such as bool ispunct(char) and bool isspace(char) // @param sentence is the string to clean // @returns string is the cleaned string. string convertToLower(string mixedcase); // Returns a lowercase version of the mixedcase string. // @param mixedcase is the string to convert. // @returns a lowercase string. string reverse(string input); // Returns a reverse version of the input string. // @param input is the string to reverse // @returns a string with the contents that's the reverse of the input. void display(vector vstrings); // Display the strings. Use a tab character to create the space seen on Mimir. // @param a vector of strings to display bool is_palindrome(string str); // Precondition: a string to be tested // Postcondition: returns a bool that is true if the input is a palindrome // returns false otherwise. Remember to add necessary include files. int main() { // ToDo: Declare two vectors of strings to save the input strings: palindromes, not_palindromes. // ToDo: Implement a loop to read in the palindrome strings using getline until the user quits. // ToDo: In the loop, call the isPalindrome function on the input string and store it in the // palindromes vector if true and the notPalindromes vector if false. // ToDo: After exiting the loop, print the list of palindromes under a Palindrome heading and // the list that are not palindromes under a Not Palindrome heading. }
What is a palindrome?
A palindrome is a string that is the same when read left to right as when read right to left. Let's write a program that determines if an arbitrary string is a palindrome. Assume that the string can have blanks, punctuation, capital letters and lower case.
Palindrome Examples:
A man, a plan, a canal, Panama.
Able was I 'ere I saw Elba.
Madam, I'm Adam.
Racecar
Sample Program Output:
Enter your palindrome or type quit:
A man, a plan, a canal, Panama.
Enter your palindrome or type quit:
Be at a time I emit a beat
Enter your palindrome or type quit:
Racecar
Enter your palindrome or type quit:
A lad named E. Mandala
Enter your palindrome or type quit:
A Toyota's a Toyota
Enter your palindrome or type quit:
Race and tell a dancer
Enter your palindrome or type quit:
quit
Palindromes:
A man, a plan, a canal, Panama.
Racecar
A lad named E. Mandala
A Toyota's a Toyota
NOT Palindromes:
Be at a time I emit a beat
Race and tell a dancer
* The space is created using a tab character.
Palindrome Algorithm
1. Make sure to keep the original string.
2. Remove all punctuation, whitespace and special characters.
3. Convert the string to lower case.
4. Create a reverse copy of the string.
5. Compare the clean version to the reverse version.
If the clean string equals the reverse, it is a palindrome
Otherwise it is not…
Make sure not to change the original string!
Program Functions
This prgram requires 6 functions:
boolean isPalindrome(string palindrome);
// isPalindrome passed the input string and calls all of the other functions to prepare the string.
// Once the string is cleaned, it tests if it's a palindrome.
// @param string the string to check.
// @returns true if it's a palindrome and false if not.
string removePunctuation(string sentence);
// Create a copy of the string and remove punctuation from the copy leaving the original alone.
// Use the cctype functions such as bool ispunct(char) and bool isspace(char)
// @param sentence is the string to clean
// @returns string is the cleaned string.
string convertToLower(string mixedcase);
// Returns a lowercase version of the mixedcase string.
// @param mixedcase is the string to convert.
// @returns a lowercase string.
string reverse(string input);
// Returns a reverse version of the input string.
// @param input is the string to reverse
// @returns a string with the contents that's the reverse of the input.
void display(vector<string> vstrings);
// Display the strings. Use a tab character to create the space seen on Mimir.
// @param a vector of strings to display
bool is_palindrome(string str);
// Precondition: a string to be tested
// Postcondition: returns a bool that is true if the input is a palindrome
// returns false otherwise.
Remember to add necessary include files.
int main() {
// ToDo: Declare two
// ToDo: Implement a loop to read in the palindrome strings using getline until the user quits.
// ToDo: In the loop, call the isPalindrome function on the input string and store it in the
// palindromes vector if true and the notPalindromes vector if false.
// ToDo: After exiting the loop, print the list of palindromes under a Palindrome heading and
// the list that are not palindromes under a Not Palindrome heading.
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 5 images