Exploring C++ 20
Functions
list2801.cpp File Reference

Listing 28-1. More...

#include <algorithm>
#include <iostream>
#include <ranges>
#include <locale>
#include <string>
#include <string_view>
Include dependency graph for list2801.cpp:

Go to the source code of this file.

Functions

bool non_letter (char ch)
 Tests for non-letter. More...
 
char lowercase (char ch)
 Converts to lowercase. More...
 
bool is_same_char (char a, char b)
 Compares two characters without regard to case. More...
 
bool is_palindrome (std::string_view str)
 Determines whether str is a palindrome. More...
 
int main ()
 Main program. More...
 

Detailed Description

Listing 28-1.

Documenting Your Code with Doxygen
Tests strings to see whether they are palindromes.

Reads lines from the input, strip non-letters, and checks whether the result is a palindrome. Ignores case differences when checking. Echoes palindromes to the standard output.

Definition in file list2801.cpp.

Function Documentation

◆ is_palindrome()

bool is_palindrome ( std::string_view  str)

Determines whether str is a palindrome.

Only letter characters are tested. Spaces and punctuation don't count. Empty strings are not palindromes because that's just too easy.

Parameters
strthe string to test
Returns
true if str is the same forward and backward and not str.empty()

Definition at line 79 of file list2801.cpp.

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 }
bool is_same_char(char a, char b)
Compares two characters without regard to case.
Definition: list2801.cpp:66
char lowercase(char ch)
Converts to lowercase.
Definition: list2801.cpp:54

◆ is_same_char()

bool is_same_char ( char  a,
char  b 
)

Compares two characters without regard to case.

Parameters
aone character to compare
bthe other character to compare
Returns
true if the characters are the same in lowercase, false if they are different.

Definition at line 66 of file list2801.cpp.

67 {
68  return lowercase(a) == lowercase(b);
69 }
char lowercase(char ch)
Converts to lowercase.
Definition: list2801.cpp:54

◆ lowercase()

char lowercase ( char  ch)

Converts to lowercase.

All conversions use the global locale.

Parameters
chthe character to test
Returns
the character converted to lowercase

Definition at line 54 of file list2801.cpp.

55 {
56  return std::tolower(ch, std::locale{});
57 }

◆ main()

int main ( )

Main program.

Sets the global locale to the user's native locale. Then imbues the I/O streams with the native locale.

Definition at line 90 of file list2801.cpp.

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_palindrome(std::string_view str)
Determines whether str is a palindrome.
Definition: list2801.cpp:79

◆ non_letter()

bool non_letter ( char  ch)

Tests for non-letter.

Tests the character ch in the global locale.

Parameters
chthe character to test
Returns
true if ch is not a letter

Definition at line 42 of file list2801.cpp.

43 {
44  return not std::isalnum(ch, std::locale{});
45 }