pangram.h
#if !defined(PANGRAM_H)
#define PANGRAM_H
#include <string_view>
namespace pangram {
bool is_pangram(std::string_view phrase);
} // namespace pangram
#endif // PANGRAM_H
pangram.cpp
#include "pangram.h"
#include <bitset>
#include <cctype>
namespace pangram {
bool is_pangram(std::string_view phrase) {
std::bitset<26> letters;
for (const auto c : phrase)
if (std::isalpha(c))
letters.set (std::tolower(c) - 'a');
return letters.all();
}
} // namespace pangram
This approach starts be defining a bitset
to keep track of the used letters.
A range-based for
loop iterates over all of the characters in the input phrase string_view
.
Each character is tested by the isalpha()
function to see if it is considered an alphabetic character according to the currently installed C locale.
If the currently installed locale is not suitable for the English alphabet, then std::locale::classic()
can be passed to the isalpha()
function defined in <locale>, like so:
#include <bitset>
#include <locale>
// code snipped
if (std::isalpha(c, std::locale::classic()))
If the character is alphabetic, then a bit value is set to represent its being used.
The ASCII value of a
is subtracted from the character tolower()
ed.
If the currently installed locale is not suitable for lowercasing English characters, then std::locale::classic()
can be passed to the tolower()
function defined in <locale>
, like so:
#include <bitset>
#include <locale>
// code snipped
letters.set (std::tolower(c, std::locale::classic()) - 'a');
For example, if the character is A
, then A
is lowered to a
, and the ASCII value of a
is subtracted by the ASCII value a
to set the bit at position 0
in the bitset
to 1
.
if the character is Z
, then Z
is lowered to z
, and the ASCII value of z
is subtracted by the ASCII value a
to set the bit at position 25
in the bitset
to 1
.
After all of the characters in the input phrase are iterated, the the all()
function is used to return if all of the 26
bits are set.