could you assist me in fixing my letters that my palindrome_addition is adding?
i added some logic to look at if the word has an even number of letter or odd and its bringing the incorrect letters still.
output that is incorrect. with words bath and aabcaa
Original string: lappal
Processed string: lappal
Line is a palindrome
Original string: lapal
Processed string: lapal
Line is a palindrome
Original string: A man, a plan, a canal, Panama!
Processed string: amanaplanacanalpanama
Line is a palindrome
Original string: lap
Processed string: lap
Line is NOT a palindrome
Characters to insert at location 0 are pa
Final line: palap
Original string: alapa
Processed string: alapa
Line is NOT a palindrome
Characters to insert at location 1 are pa
Final line: apalapa
Original string: bath
Processed string: bath
Line is NOT a palindrome
Characters to insert at location 0 are hht
Final line: hhtbath
Original string: aabcaa
Processed string: aabcaa
Line is NOT a palindrome
Characters to insert at location 2 are cab
Final line: aacabbcaa
Original string: abc...123...cba
Processed string: abc123cba
Line is NOT a palindrome
Characters to insert at location 3 are 32
Final line: abc32123cba
The correct output i should get.
Original line: lappal
Processed line: lappal
Line is a palindrome
Original line: lapal
Processed line: lapal
Line is a palindrome
Original line: A man, a plan, a canal, Panama!
Processed line: amanaplanacanalpanama
Line is a palindrome
Original line: lap
Processed line: lap
Line is NOT a palindrome
Characters to insert at location 0 are pa
Final line: palap
Original line: alapa
Processed line: alapa
Line is NOT a palindrome
Characters to insert at location 1 are pa
Final line: apalapa
Original line: bath
Processed line: bath
Line is NOT a palindrome
Characters to insert at location 0 are hta
Final line: htabath
Original line: aabcaa
Processed line: aabcaa
Line is NOT a palindrome
Characters to insert at location 2 are c
Final line: aacbcaa
Original line: abc...123...cba
Processed line: abc123cba
Line is NOT a palindrome
Characters to insert at location 3 are 32
Final line: abc32123cba
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
usingnamespace std;
string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);
int main()
{
string line;
while (getline (cin, line) )
{
cout<<"Original string: " << line<< endl;
cout<<"Processed string: " << process(line)<< endl;
//cout<<"size of process string " << process(line).size();
if ( is_palindrome(process(line)) )
{
cout<<"Line is a palindrome"<< endl;
}
else
{
cout<<"Line is NOT a palindrome"<< endl;
cout<<"Characters to insert at location "
<<palindrome_fix_location(process(line))
<<" are "<<palindrome_addition( process(line), palindrome_fix_location(process(line) ) )
<<endl;
cout<<"Final line: "<<process(line).insert(palindrome_fix_location(process(line)),
palindrome_addition( process(line), palindrome_fix_location(process(line)) ) )
<<endl;
}
cout<<endl;
}
return 0;
}
/*
returns a string such that it is in lower case and contains no
punctuation or spacing
*/
string process(string edited)
{
string newstr = "";
for (unsignedint i = 0 ; i <= edited.size() - 1 ; i++)
if (isalnum(edited[i]))
{
newstr += tolower(edited[i]);
}
return newstr;
}
bool is_palindrome(string edited)
{
if (edited.size() == 0 || edited.size() == 1)
returntrue;
else
{
if (edited[0] != edited[edited.size() - 1])
returnfalse;
elsereturn (is_palindrome(edited.substr(1, edited.size() - 2)));
}
}
int palindrome_fix_location(string edited)
{
int count = 0;
for(unsignedint i = 0; i < (edited.size() / 2); i++)
{
if (edited[i] != edited[edited.size() - 1 - i])
{
count += (i);
break;
}
}
return count;
}
string palindrome_addition(string edited, int n)
{
string letter = "";
int counted = 0;
for(unsignedint i = n, j = edited.size() - 1 - n; i < edited.size(); i++, j--)
{
if (edited[i] != edited[j] && edited.size() % 2 == 0)
{
letter += edited[j];
letter += edited[j - i];
}
else
letter += edited[j];
letter += edited[j - 1];
break;
}
return letter;
/*
given a string and the location where a
string should be added to make it a palindrome, this should return the text that needs to be added
at that location
*/
}