Palindrone


I wrote this code out but it does not seem to compile. it is a palindrome for checking the same string front and back.

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void trim(string& x) {
int k = 0;
while (k < x.size() && (x[k] == ' ' || x[k] == '\t' || x[k] == '\n' || x[k] == ',' || x[k] == '!' || x[k] == '?' || x[k] == '""'))
k++;
x.erase(0, k);

int s = x.size();
while (s>0 && (x[s - 1] == ' ' || x[s - 1] == '\t' || x[s - 1] == '\n' || x[s - 1] == '?' || x[s - 1] == '!' || x[s - 1] == '""'))
s--;
x.erase(s);
}
string deletereverse(string &s)
{
s.erase(remove(s.begin(), s.end(), ' '), s.end());
s.erase(remove(s.begin(), s.end(), ','), s.end());
s.erase(remove(s.begin(), s.end(), "''"), s.end());
cout << s << "----------- Reverse String" << endl;
return s;
}
string delSpaces(string &x)
{
x.erase(remove(x.begin(), x.end(), ' '), x.end());
x.erase(remove(x.begin(), x.end(), ','), x.end());
x.erase(remove(x.begin(), x.end(), "''"), x.end());
cout << x << "----------- Actual String" << endl;
return x;
}

bool isPalindrome(const string &x, const string&s)
{
if (x == s)
{
return true;
}
return false;
}

int main()
{
string x;

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

for (int i = 0; i < x.length(); i++) {
if ('a' <= x[i] && x[i] <= 'z') {
x[i] = char(((int)x[i]) - 32);
}

if ('A' <= x[i] && x[i] <= 'Z') {
x[i] = char(((int)x[i]) + 32);
}
}

trim(x);
string s = x;
reverse(s.begin(),s.end());
cout << endl;
deletereverse(s);
cout << endl;
delSpaces(x);
cout << endl;
cout << isPalindrome(x,s);
cout << endl;

system("pause");
return 0;
}
If it doesn't compile, your compiler tells you why. What did your compiler say?
The compiler says something like conversion failed from char* to int. I'm not sure why but it's related to the function deletereverse line 3 where I'm using '""'.
Top tip for the future; rather than tell us what the compiler error is like, tell us what it actually is. The exact, actual error. It will generally have a line number in it so you can identify the line of code containing the error. Programming demands precision; not just what things are like.

s.erase(remove(s.begin(), s.end(), "''"), s.end());

See that bit I've put in bold and underlined? That's meant to be a char. A single char. You appear to have created a char-pointer, pointing to a string of chars, which is this: ''

That's two chars in the string. What are you trying to do?

The complier is expecting a single char; you're giving it a char-pointer to a string of chars. A char-pointer is not the same thing as a char.
Last edited on
Alright. It makes sense. I have a string (a for apple 'n' b for ball). How would I delete the ('n') two char character quotation marks n get a string such as (a for apple n b for ball). Thanks you. This helped me understand about char.
Topic archived. No new replies allowed.