-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (39 loc) · 1.01 KB
/
main.cpp
File metadata and controls
45 lines (39 loc) · 1.01 KB
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
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <cstring>
bool is_palindrome(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_palindrome(const T& str)
{
return std::equal(begin(str), end(str), rbegin(str));
}
template<size_t N>
bool is_palindrome(const char (&str)[N])
{
return is_palindrome(str, N - 1);
}
template<size_t N>
void test(const char (&txt)[N])
{
const std::string str(txt);
std::cout << str << " is palindrome (const char*): " << is_palindrome(str.c_str(), str.size()) << "\n";
std::cout << str << " is palindrome (std::string): " << is_palindrome(str) << "\n";
std::cout << str << " is palindrome (const char[]): " << is_palindrome(txt) << "\n";
}
int main()
{
std::cout << std::boolalpha;
test("");
test("ana");
test("animal lamina");
test("aaaa");
test("Ojo");
test("HeaderFiles");
}