Avatar of DerboTurbo

DerboTurbo's solution

to Pangram in the C++ Track

Published at Oct 20 2019 · 0 comments
Test suite

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.


This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.


Wikipedia https://en.wikipedia.org/wiki/Pangram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#include "pangram.h"
#include "test/catch.hpp"


    REQUIRE(pangram::is_pangram("the quick brown fox jumps over the lazy dog"));

    REQUIRE(!pangram::is_pangram("a quick movement of the enemy will jeopardize five gunboats"));

    REQUIRE(!pangram::is_pangram("the quick brown fish jumps over the lazy dog"));


    REQUIRE(pangram::is_pangram("the 1 quick brown fox jumps over the 2 lazy dogs"));

    REQUIRE(!pangram::is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));

    REQUIRE(pangram::is_pangram("\"Five quacking Zephyrs jolt my wax bed.\""));

    REQUIRE(!pangram::is_pangram("the quick brown fox jumps over with lazy FX"));


#include "pangram.h"
#include <string>
#include <algorithm>

namespace pangram {

	bool is_pangram(const std::string& sentence){
		std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
		/*will not have duplicates, digits, punctuation, 
		spaces or capital letters when checked against alphabet.*/
		std::string check = sentence; 

		transform(check.begin(),check.end(),check.begin(),tolower); //make all characters lowercase

		auto end = std::remove_if(check.begin(), check.end(), [](char c) { return isspace(c) || ispunct(c) || isdigit(c);}); //move spaces, punctuation and numbers to back of string
		check.erase(end, check.end()); //erase from where the spaces, puncts and digits start in string to end of string 

		std::sort(check.begin(), check.end()); //sort of the string alphabetically by each character
		auto res = std::unique(check.begin(), check.end()); //duplicates to end of string
		check.erase(res, check.end()); 

		return check == alphabet; 
}  // namespace pangram


#if !defined(PANGRAM_H)
#define PANGRAM_H
#include <string>
#include <algorithm>

namespace pangram {

	bool is_pangram(const std::string& sentence);

}  // namespace pangram

#endif // PANGRAM_H

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?