Avatar of shmibs

shmibs's solution

to Pangram in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 "Guides" section of the C track on the Exercism site. 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.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need 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, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

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.

test_pangram.c

#include "vendor/unity.h"
#include "../src/pangram.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_null(void)
{
   TEST_ASSERT_FALSE(is_pangram(NULL));
}

void test_empty_sentence(void)
{
   TEST_IGNORE();               // delete this line to run test
   const char sentence[] = "";

   TEST_ASSERT_FALSE(is_pangram(sentence));
}

void test_perfect_lowercase_pangram(void)
{
   TEST_IGNORE();
   const char sentence[] = "abcdefghijklmnopqrstuvwxyz";

   TEST_ASSERT_TRUE(is_pangram(sentence));
}

void test_lowercase_pangram(void)
{
   TEST_IGNORE();
   const char sentence[] = "the quick brown fox jumps over the lazy dog";

   TEST_ASSERT_TRUE(is_pangram(sentence));
}

void test_missing_character_x(void)
{
   TEST_IGNORE();
   const char sentence[] =
       "a quick movement of the enemy will jeopardize five gunboats";

   TEST_ASSERT_FALSE(is_pangram(sentence));
}

void test_another_missing_h(void)
{
   TEST_IGNORE();
   const char sentence[] = "five boxing wizards jump quickly at it";

   TEST_ASSERT_FALSE(is_pangram(sentence));
}

void test_pangram_with_underscores(void)
{
   TEST_IGNORE();
   const char sentence[] = "the_quick_brown_fox_jumps_over_the_lazy_dog";

   TEST_ASSERT_TRUE(is_pangram(sentence));
}

void test_pangram_with_numbers(void)
{
   TEST_IGNORE();
   const char sentence[] = "the 1 quick brown fox jumps over the 2 lazy dogs";

   TEST_ASSERT_TRUE(is_pangram(sentence));
}

void test_missing_letters_replaced_by_numbers(void)
{
   TEST_IGNORE();
   const char sentence[] = "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog";

   TEST_ASSERT_FALSE(is_pangram(sentence));
}

void test_mixed_case_and_punctuation(void)
{
   TEST_IGNORE();
   const char sentence[] = "\"Five quacking Zephyrs jolt my wax bed.\"";

   TEST_ASSERT_TRUE(is_pangram(sentence));
}

void test_upper_and_lower_case_of_same_character(void)
{
   TEST_IGNORE();
   const char sentence[] = "the quick brown fox jumps over with lazy FX";

   TEST_ASSERT_FALSE(is_pangram(sentence));
}

int main(void)
{
   UnityBegin("test/test_pangram.c");

   RUN_TEST(test_null);
   RUN_TEST(test_empty_sentence);
   RUN_TEST(test_perfect_lowercase_pangram);
   RUN_TEST(test_lowercase_pangram);
   RUN_TEST(test_missing_character_x);
   RUN_TEST(test_another_missing_h);
   RUN_TEST(test_pangram_with_underscores);
   RUN_TEST(test_pangram_with_numbers);
   RUN_TEST(test_missing_letters_replaced_by_numbers);
   RUN_TEST(test_mixed_case_and_punctuation);
   RUN_TEST(test_upper_and_lower_case_of_same_character);

   UnityEnd();
   return 0;
}
#include "pangram.h"

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>

bool is_pangram(const char *s)
{
	unsigned acc = 0;
	unsigned char c;

	if (s == NULL)
		return false;

	for (;s[0] != '\0'; s++) {
		c = toupper(s[0]);

		if (isalpha(c))
			acc |= 1 << (c - 'A');
	}

	/* one bit for every letter of the alphabet */
	return acc == 0x3ffffff;
}

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?