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

fix my c++ program. it\'s about palindrome. it needs to be NOT case sensitive(i

ID: 665662 • Letter: F

Question

fix my c++ program. it's about palindrome.

it needs to be NOT case sensitive(i converted it to lower case);

space does not matter;

alphanumeric is vaild

eliminate punctuation.

for example of valid palindromes:

M o m!

n o..!# on

i'm Adam!

#include <iostream>
#include <string>
#include <ctype>
void punct()//delete punctuations
{
string phrase
for (int i = 0, i < phrase.size(); i++)
{
if (ispunct(phrase[i]))
{
phrase.erase(i--, 1);
}
}
return (phrase);
}


void display_lower (string phrase, char c)//to change to lower case
{
string phrase;
for (i=0; i<phrase.size();i++)
{
char str[]= phrase;
char c;
while (str[i])
{
c = str[i];
if (isupper(c))
{
c=tolower(c);
i++;
}
return (c);
}
}
}
string gsub(string str,string target)//to remove space
{
int p;
string target;
while ((p = str.find(target)) != string::np)
{
leftstr = str.substr(0,p);
rightstr = str.substr(p+target.size())
str = leftstr + rightstr;
}
return (str);
}

bool even(string phrase)// to check if leading and trailing characters are the same
{
if(phrase.size()%2==0)
{
return (true);
else
return (false);
}
}

bool isPalindrome(string phrase)
{
bool even(string phrase);
if(even(phrase))
{
for(int i=0;i<(phrase.size()/2);i++)
{
for(int j=phrase.size()-1;j>(phrase.size()/2);j--)
{
if(phrase[i]!= phrase[j])
{
return (false);
else
return (true);
}
}
}
}
}


int main()
{

string phrase;

cout<<"Enter a phrase: ";
getline(cin,phrase);

if(isPalindrome(phrase))

cout<<phrase<<" is palindrome"<<endl;
else
cout<<phrase<<" is not palindrome"<<endl;

return (0);
}

Explanation / Answer

#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
string punct(string phrase)//delete punctuations
{
    string lowerString="";
    for (int i = 0; i < phrase.size(); i++)
    {
        if (!ispunct(phrase[i]))
        {
            lowerString=lowerString+phrase[i];
        }
    }
    return (lowerString);
}
string lowerString (string phrase)//to change to lower case
{
    locale loc;
    string lowerString="";
    for (int i=0; i<phrase.size();i++)
    {
      lowerString=lowerString+tolower(phrase[i],loc);
    }
    return lowerString;
}
string gsub(string phrase)
{
string lowerString="";
for (int i=0; i<phrase.size();i++)
    {
      if(!isspace(phrase[i]))
        lowerString=lowerString+phrase[i];
    }
    return lowerString;
}

bool isPalindrome(string phrase)
{
// You need not check if the string is even length because an odd length string might also be a palindrome
// For ex: abkba this is a palindrome
phrase=gsub(phrase);
phrase=punct(phrase);
phrase=lowerString(phrase);
for(int i=0;i<(phrase.length()/2);i++)
{
    int j=phrase.length()-1-i;
    if(phrase[i]!= phrase[j])
    {
      return (false);
    }
}
return true;
}      


int main()
{
string phrase;
cout<<"Enter a phrase: ";
getline(cin,phrase);
if(isPalindrome(phrase))
        cout<<phrase<<" is palindrome"<<endl;
else
        cout<<phrase<<" is not palindrome"<<endl;
return (0);
}