Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (40 sloc) 1.02 KB
#include <iostream>
#include <string>
#include <cstring>
bool is_palindrom(const char* str, size_t len)
{
if (len == 0) return true;
for (size_t ii = 0, jj = len - 1; ii < jj; ++ii, --jj) {
if (str[ii] != str[jj]) return false;
}
return true;
}
template<typename T>
bool is_palindrom(const T& str)
{
return std::equal(begin(str), end(str), rbegin(str));
}
template<size_t N>
bool is_palindrom(const char (&str)[N])
{
return is_palindrom(str, N - 1);
}
template<size_t N>
void test(const char (&txt)[N])
{
const std::string str(txt);
std::cout << str << " is palindrom (const char*): " << is_palindrom(str.c_str(), str.size()) << "\n";
std::cout << str << " is palindrom (std::string): " << is_palindrom(str) << "\n";
std::cout << str << " is palindrom (const char[]): " << is_palindrom(txt) << "\n";
}
int main()
{
std::cout << std::boolalpha;
test("");
test("ana");
test("animal lamina");
test("aaaa");
test("Ojo");
test("HeaderFiles");
return 0;
}
You can’t perform that action at this time.