Exploring C++ 20
list2801.cpp
Go to the documentation of this file.
1 
28 #include <algorithm>
29 #include <iostream>
30 #include <ranges>
31 #include <locale>
32 #include <ranges>
33 #include <string>
34 #include <string_view>
35 
42 bool non_letter(char ch)
43 {
44  return not std::isalnum(ch, std::locale{});
45 }
46 
54 char lowercase(char ch)
55 {
56  return std::tolower(ch, std::locale{});
57 }
58 
66 bool is_same_char(char a, char b)
67 {
68  return lowercase(a) == lowercase(b);
69 }
70 
79 bool is_palindrome(std::string_view str)
80 {
81  auto filtered_str{ str | std::views::filter(lowercase) };
82  return std::ranges::equal(filtered_str, filtered_str|std::views::reverse,
83  is_same_char);
84 }
85 
90 int main()
91 {
92  std::locale::global(std::locale{""});
93  std::cin.imbue(std::locale{});
94  std::cout.imbue(std::locale{});
95 
96  for (std::string line{}; std::getline(std::cin, line); /*empty*/)
97  if (is_palindrome(line))
98  std::cout << line << '\n';
99 }
bool is_same_char(char a, char b)
Compares two characters without regard to case.
Definition: list2801.cpp:66
bool non_letter(char ch)
Tests for non-letter.
Definition: list2801.cpp:42
int main()
Main program.
Definition: list2801.cpp:90
char lowercase(char ch)
Converts to lowercase.
Definition: list2801.cpp:54
bool is_palindrome(std::string_view str)
Determines whether str is a palindrome.
Definition: list2801.cpp:79