Determine if a word or phrase is an isogram.
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.
Examples of isograms:
The word isograms, however, is not an isogram, because the s repeats.
Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
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.
Wikipedia https://en.wikipedia.org/wiki/Isogram
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
#include "vendor/unity.h"
#include "../src/isogram.h"
#include <stdlib.h>
void setUp(void)
{
}
void tearDown(void)
{
}
void test_empty_string(void)
{
TEST_ASSERT_TRUE(is_isogram(""));
}
void test_lower_case_only(void)
{
TEST_IGNORE(); // delete this line to run test
TEST_ASSERT_TRUE(is_isogram("isogram"));
}
void test_duplicated_letter(void)
{
TEST_IGNORE();
TEST_ASSERT_FALSE(is_isogram("eleven"));
}
void test_longest_known_isogram(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_isogram("subdermatoglyphic"));
}
void test_duplicated_letter_mixed_case(void)
{
TEST_IGNORE();
TEST_ASSERT_FALSE(is_isogram("Alphabet"));
}
void test_non_letter_char(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_isogram("thumbscrew-japingly"));
}
void test_duplicated_non_letter_char(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_isogram("Hjelmqvist-Gryb-Zock-Pfund-Wax"));
}
void test_multiple_whitespace(void)
{
TEST_IGNORE();
TEST_ASSERT_TRUE(is_isogram("Emily Jung Schwartzkopf"));
}
void test_duplicated_letter_within_word(void)
{
TEST_IGNORE();
TEST_ASSERT_FALSE(is_isogram("accentor"));
}
int main(void)
{
UnityBegin("test/test_isogram.c");
RUN_TEST(test_empty_string);
RUN_TEST(test_lower_case_only);
RUN_TEST(test_duplicated_letter);
RUN_TEST(test_longest_known_isogram);
RUN_TEST(test_duplicated_letter_mixed_case);
RUN_TEST(test_non_letter_char);
RUN_TEST(test_duplicated_non_letter_char);
RUN_TEST(test_multiple_whitespace);
RUN_TEST(test_duplicated_letter_within_word);
UnityEnd();
return 0;
}
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#define CCOUNT ('z' - 'a' + 1)
bool is_isogram(const char phrase[])
{
bool letters[CCOUNT] = {false};
int p_len = strlen(phrase);
int i, c, k;
for (i = 0; i < p_len; i++) {
c = tolower(phrase[i]);
if (c == ' ' || c == '-')
continue;
if (!isalpha(c))
return false;
k = c - 'a';
if (letters[k])
return false;
letters[k] = true;
}
return true;
}
#ifndef ISOGRAM_H
#define ISOGRAM_H
#include <stdbool.h>
bool is_isogram(const char phrase[]);
#endif
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments