Code for checking palindrome doesn't compile

Jun 3, 2013 at 9:41pm
The error I am receiving is that "palindrome.cpp:14: error: no match for âoperator==â in âreversechar(((std::string&)(& line)), 0u, (line.std::basic_string<_CharT, _Traits, _Alloc>::length [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]() - 1ul)) == lineâ" I don't know what this is and how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

void swap(char &a, char&b){int temp = a; a=b; b= temp;}
void reversechar(string &str, size_t start, size_t end);


int main()
{
string line;
cout << "Please enter a string to check for palindrome" << endl;
getline(cin, line);
        if(reversechar(line, 0, line.length()-1) == line)
        {
                cout << "palindrome" << endl;
        }
        else
        {
                cout << "not palindrome" << endl;
        }
        return 0;
}


void reversechar(string &str, size_t start, size_t end)
{
        for(int i = 0; i<= (end-start)/2; i++)
        {
                swap(str[start + i], str[end - i]);
        }
}

~
Jun 3, 2013 at 9:46pm
In line 14 you are trying to compare string (line) and void result of reversechar function.

The compiler doesn't know how to do it.
Jun 3, 2013 at 9:49pm
You declared function reversechar as returning void

void reversechar(string &str, size_t start, size_t end);

after that you are trying to compare void with an object of type std:;string

if(reversechar(line, 0, line.length()-1) == line)

The simplest way to check that a string is a palindrome is to write just one statement

if ( line == string( line.rbegin(), line.rend() ) )



Jun 3, 2013 at 9:50pm
Thanks. I completely overlooked that. I changed it to return type String and changed it for the prototype and now is working.
Jun 3, 2013 at 9:56pm
You are wrong.Your function works incorrectly. You are not comparing the original string with the reversed string. You always compare the reversed string with itself.:) It is because you pass the original string by reference to the function.
Jun 3, 2013 at 10:07pm
How is the function working incorrectly, when the output I receive is exactly what I need?
Jun 3, 2013 at 10:15pm
I already wrote you why the function works incorrectly. You always compare it with itself. Check the function entering for example "Hello"
Jun 3, 2013 at 10:43pm
Yea thanks, I tested it out and it doesn't work. I removed the function and changed the if condition to line == string( line.rbegin(), line.rend() ) and now it's working.
Jun 3, 2013 at 11:28pm
You could update your own function that it would work. For example

1
2
3
4
5
6
7
8
9
10
11
string reversechar( const string &str, size_t start, size_t end)
{
        string t( str, start, end - start + 1 );

        for(int i = 0; i<= (end-start)/2; i++)
        {
                swap(t[i], t[end - start - i]);
        }

        return t;
}



Topic archived. No new replies allowed.