#include <iomanip>
#include <iostream>
#include <algorithm>
#include <string>
bool is_palindrome(const std::string& str)
{
std::string copy(str);
std::transform(copy.begin(), copy.end(), std::tolower);
if (copy.size() == 0)
{
return true;
}
auto rfirst = copy.rbegin();
auto rlast = --copy.rend();
while (rfirst != rlast)
{
if (*rfirst++ != *rlast--)
return false;
}
return true;
}
int main()
{
std::string str{ "KArak" };
std::cout << std::boolalpha;
std::cout << "is \"KArak\" palindrome? " << is_palindrom(str) << '\n';
std::cout << "is \"fuf\" palindrome? " << is_palindrom("fuf") << '\n';
std::cout << "is empty string palindrome? " << is_palindrom("") << '\n';
return 0;
}
I2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHN0cmluZz4KCmJvb2wgaXNfcGFsaW5kcm9tZShjb25zdCBzdGQ6OnN0cmluZyYgc3RyKQp7CglzdGQ6OnN0cmluZyBjb3B5KHN0cik7CglzdGQ6OnRyYW5zZm9ybShjb3B5LmJlZ2luKCksIGNvcHkuZW5kKCksIHN0ZDo6dG9sb3dlcik7CgkKCWlmIChjb3B5LnNpemUoKSA9PSAwKQoJewoJCXJldHVybiB0cnVlOwoJfQoJCiAgICBhdXRvIHJmaXJzdCA9IGNvcHkucmJlZ2luKCk7CglhdXRvIHJsYXN0ID0gLS1jb3B5LnJlbmQoKTsKCXdoaWxlIChyZmlyc3QgIT0gcmxhc3QpCgl7CgkgICAgaWYgKCpyZmlyc3QrKyAhPSAqcmxhc3QtLSkKCSAgICAgICAgcmV0dXJuIGZhbHNlOwoJfQoKCXJldHVybiB0cnVlOwp9CgppbnQgbWFpbigpCnsKICAgIHN0ZDo6c3RyaW5nIHN0cnsgIktBcmFrIiB9OwogICAgc3RkOjpjb3V0IDw8IHN0ZDo6Ym9vbGFscGhhOwogICAgc3RkOjpjb3V0IDw8ICJpcyBcIktBcmFrXCIgcGFsaW5kcm9tZT8gIiA8PCBpc19wYWxpbmRyb20oc3RyKSA8PCAnXG4nOwogICAgc3RkOjpjb3V0IDw8ICJpcyBcImZ1ZlwiIHBhbGluZHJvbWU/ICIgPDwgaXNfcGFsaW5kcm9tKCJmdWYiKSA8PCAnXG4nOwogICAgc3RkOjpjb3V0IDw8ICJpcyBlbXB0eSBzdHJpbmcgcGFsaW5kcm9tZT8gIiA8PCBpc19wYWxpbmRyb20oIiIpIDw8ICdcbic7CiAgICByZXR1cm4gMDsKfQ==
prog.cpp: In function ‘bool is_palindrome(const string&)’:
prog.cpp:9:55: error: no matching function for call to ‘transform(std::__cxx11::basic_string<char>::iterator, std::__cxx11::basic_string<char>::iterator, <unresolved overloaded function type>)’
std::transform(copy.begin(), copy.end(), std::tolower);
^
In file included from /usr/include/c++/6/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/6/bits/stl_algo.h:4166:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
transform(_InputIterator __first, _InputIterator __last,
^~~~~~~~~
/usr/include/c++/6/bits/stl_algo.h:4166:5: note: template argument deduction/substitution failed:
prog.cpp:9:55: note: candidate expects 4 arguments, 3 provided
std::transform(copy.begin(), copy.end(), std::tolower);
^
In file included from /usr/include/c++/6/algorithm:62:0,
from prog.cpp:3:
/usr/include/c++/6/bits/stl_algo.h:4203:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
transform(_InputIterator1 __first1, _InputIterator1 __last1,
^~~~~~~~~
/usr/include/c++/6/bits/stl_algo.h:4203:5: note: template argument deduction/substitution failed:
prog.cpp:9:55: note: candidate expects 5 arguments, 3 provided
std::transform(copy.begin(), copy.end(), std::tolower);
^
prog.cpp: In function ‘int main()’:
prog.cpp:31:65: error: ‘is_palindrom’ was not declared in this scope
std::cout << "is \"KArak\" palindrome? " << is_palindrom(str) << '\n';
^