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

Improve my palindrome solution: Write a function that checks if a given sentence

ID: 671817 • Letter: I

Question

Improve my palindrome solution:

Write a function that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.

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

string input;
string inputcopy;
bool result;

int main() //Palindrome Function
{
   getline(cin, input);
   transform(input.begin(), input.end(), input.begin(), tolower);
   input.erase(remove_if(input.begin(), input.end(), ispunct), input.end());
   input.erase(remove(input.begin(), input.end(), ' '), input.end());
   inputcopy = input;
   reverse(inputcopy.begin(), inputcopy.end());

   if (input == inputcopy)
   {
       cout << "Result= YES Palindrome" << endl;
       result = true;
   }

   else
   {
       cout << "Result= NO Palindrome" << endl;
       result = false;
   }
   return 0;
}

Explanation / Answer

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string input;
string inputcopy;
bool result;
bool palindrome(string input) //Palindrome Function
{
transform(input.begin(), input.end(), input.begin(), tolower);
input.erase(remove_if(input.begin(), input.end(), ispunct), input.end());
input.erase(remove(input.begin(), input.end(), ' '), input.end());
inputcopy = input;
reverse(inputcopy.begin(), inputcopy.end());
if (input == inputcopy)
{
cout << "Result= YES Palindrome" << endl;
result = true;
}
else
{
cout << "Result= NO Palindrome" << endl;
result = false;
}
return 0;
}