can you give me the matching function for the call to ‘ispalindrome’?

The Problem

Your isPalindrome function expects char * for its argument. There is no suitable implicit conversion (operator or otherwise) from what you’re giving it, a std::string, to that char *. The compiler is dutifully pressing on and attempting to find an alternative overload of isPalindrome that matches what you’re actually feeding it. Finding none, the resulting error halts the compile and leaves you with your not-necessarily-crystal-clear error message.

A Solution

There are a multitude of solutions, but the easiest is simply to change isPalindrome to accept a reference to const std::string:

bool isPalindrome(std::string const& s)
    std::size_t n = s.length();
    for (std::size_t i=0; i<n/2; ++i)
        if(s[i] != s[n - i - 1])
            return false;
    return true;

A More-C++ Solution

All you’re really doing is comparing two ranges. The standard library provides a function to do this, std::equal , which takes an iterator begin/end, and a secondary iterator starting point (and after C++14, a secondary ending point). Furthermore, the std::string class provides both fore and aft direction iterators (begin() and rbegin(), and the lot of them can be used to do this:

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

bool isPalindrome(std::string const& s)
    return std::equal(s.begin(), std::next(s.begin(), s.length()/2), s.rbegin());

Note that neither this, nor your original C-based approach, will work with mixed-case comparison. I.e. Aba will certainly not compare as a successful palindrome. More work is involved if you want that. Also, the three-argument overload of std::equal is available until, but not including, C++20, after that you need the four argument version (the second range also requires an end-iterator; not just a starting point).


Neither #include <bits/stdc++.h> or using namespace std are good ideas, and should be avoided. There are woefully inadequate web sites and online “courses” that preach using both almost by reflex; just… don’t. If you want know more about why these are terrible practices I urge you to read:

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top