C++ Tasks Write a function that takes a string as an argument and returns a bool
ID: 3685593 • Letter: C
Question
C++
Tasks
Write a function that takes a string as an argument and returns a boolean. The function should return true if the string is a palindrome and return false if the string is not a palindrome. Allow the user to repeatedly enter messages to check if they are palindromes. Be sure to allow the user to have spaces in their message.
Notes
You may find that the getline function you’ve used in the past isn’t compatible with strings. Instead, try getline(cin, your_string_variable). (When we were working with cstrings, we used cin.getline(your_cstring_variable, number_of_characters_to_get).)
You might have problems using both >> and getline to get input from the user. Use cin.ignore() right after an extraction (>>) to clear out any extraneous whitespace so that you don’t trip getline up. You can compare strings just like many other variables with ==.
Unlike in previous labs, a prototype for the palindromCheck function hasn’t been included. See if you can figure out what you should use.
Sample Output (note, your array might be different and that will affect your output)
Enter a message to check: A man, a plan, a canal - Panama! amanaplanacanalpanama vs amanaplanacanalpanama
That's a palindrome!
Would you like to try another message (0 or 1)? 1
Enter a message to check: Is this a palindrome?
isthisapalindrome vs emordnilapasihtsi
That's not a palindrome.
Would you like to try another message (0 or 1)? 1
Enter a message to check: Never odd or even.
neveroddoreven vs neveroddoreven
That's a palindrome!
Would you like to try another message (0 or 1)? 0
Explanation / Answer
#include #include #include using namespace std; bool doYouWantToPlayFirst(string question); int main () { char humanPiece; char computerPiece; doYouWantToPlayFirst("Do you want to take the first move? : "); { humanPiece = 'X'; computerPiece = 'O'; } else { humanPiece = 'O'; computerPiece = 'X'; return 0; } bool doYouWantToPlayFirst(string question) { char first; cout first; first = toupper(first); if ((first != 'Y') && (first != 'N')) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.