Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Palindrome in C++ Modify the program so it takes a WORD and a PHRASE and returns

ID: 3880688 • Letter: P

Question

Palindrome in C++

Modify the program so it takes a WORD and a PHRASE and returns the correct feedback. You entered a WORD or you ENTERED a PHRASE.

#include <iostream>
#include <string>
using namespace std;

// Function prototype
bool isPalindrome(string);

int main()
{
const int SIZE = 6;

    // Create an array of strings to test.
    string testStrings[SIZE] =
         { "ABLE WAS I ERE I SAW ELBA",
              "FOUR SCORE AND SEVEN YEARS AGO",
              "NOW IS THE TIME FOR ALL GOOD MEN",
              "DESSERTS I STRESSED",
              "AKS NOT WHAT YOUR COUNTRY CAN DO FOR YOU",
              "KAYAK" };
  
   // Test the strings.
   for (int i = 0; i < SIZE; i++)
   {
      cout << """ << testStrings[i] << """;
   if (isPalindrome(testStrings[i]))
    cout << " is a palindrome. ";
   else
    cout << " is NOT a palindrome. ";
   }

return 0;
}

//*******************************************
// The isPalindrome function returns true   *
// the argument is a palindrome, false      *
// otherwise.                               *
//*******************************************

bool isPalindrome(string str)
{
   bool status = false;

   if (str.length() <= 1)
      status = true;
   else if (str.at(0) == str.at(str.length()-1))
      status = isPalindrome (str.substr(1, str.length()-2));

   return status;
}

Assignment 2 - C++ Palindrome Description The challenge for this second assignment is to develop a program, called palindrome.cpp, capable of detecting palindromes. A palindrome is a word, phrase, or sequence that reads the same backward as forward. Your program should take input from stdin and determine whether the characters formulate a palindrome Requirements The following functions should be defined: bool isPalindrome (string text) Example $/palindrome Enter Text: bob The word "bob" is a palindrome. Enter Text: A car, a man,a maraca The phrase "A car, a man, a maraca" is a palindrome. Enter Text: slow The word "slow" is not a palindrome. Enter Text: quit Due: January 29th, 2017 11:59 PM

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

// Function prototype
bool isPalindrome(string);

int main()
{
const int SIZE = 6;


string s;
cout<<"Enter the WORD or PHRASE: "<<endl;
getline(cin, s);
  

if (isPalindrome(s))
cout << s<<" is a palindrome. ";
else
cout << s<<" is NOT a palindrome. ";


return 0;
}

//*******************************************
// The isPalindrome function returns true *
// the argument is a palindrome, false *
// otherwise. *
//*******************************************

bool isPalindrome(string str)
{
bool status = false;

if (str.length() <= 1)
status = true;
else if (str.at(0) == str.at(str.length()-1))
status = isPalindrome (str.substr(1, str.length()-2));

return status;
}

Output: