(Palindromes) A palindrome is a string that is spelled the same way forward and backward. Examples of palindromes include “radar” and “able was i ere i saw elba.” Write a recursive function testPalindrome that returns true if a string is a palindrome, and false otherwise. Note that like an array the square brackets ([]) operator can be used to iterate through the characters in a string.
What will be an ideal response?
```
#include
#include
using namespace std;
bool testPalindrome( string, int, int );
int main()
{
string input;
// get sentence to test from user
cout << "Enter a sentence:\n";
getline( cin, input );
// Print whether or not sentence is palindrome
if ( testPalindrome( input, 0, input.length() - 1 ) )
cout << '\"' << string << "\" is a palindrome" << endl;
else
cout << '\"' << string << "\" is not a palindrome" << endl;
} // end main
// function to see if sentence is a palindrome
bool testPalindrome( string palindrome, int left, int right )
{
// test array to see if a palindrome
if ( left == right || left > right )
return true;
else if ( palindrome[ left ] != palindrome[ right ] )
return false;
else
return testPalindrome( palindrome, left + 1, right - 1 );
} // end function testPalindrome
```
Enter a sentence:
a man a plan a canal panama
"a man a plan a canal panama" is a palindrome
Enter a sentence:
c++ is fun
"c++ is fun" is not a palindrome
You might also like to view...
Method_______ adds a new node to the top of the stack.
a) add. b) insert. c) push. d) None of the above.
An older type of serial interface connector.
What will be an ideal response?
What are two valid steps in the six step troubleshooting method discussed in this chapter?
A. Boot to Safe Mode B. Establish a Theory C. Document outcomes D. Reinstall Windows
When formatting a subform, the Form view provides a more accurate preview than Design view
Indicate whether the statement is true or false