Convert a number to a string, the contents of which depend on the number's factors.
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.
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.
A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
#include "vendor/unity.h"
#include "../src/raindrops.h"
#include <stdlib.h>
#define BUFFER_LENGTH 16
void setUp(void)
{
}
void tearDown(void)
{
}
static void convert_drops(int drops, char *expected)
{
char result[BUFFER_LENGTH] = { 0 };
convert(result, drops);
TEST_ASSERT_EQUAL_STRING(expected, result);
}
static void test_one_yields_itself(void)
{
convert_drops(1, "1");
}
static void test_three_yields_pling(void)
{
TEST_IGNORE(); // delete this line to run test
convert_drops(3, "Pling");
}
static void test_five_yields_plang(void)
{
TEST_IGNORE();
convert_drops(5, "Plang");
}
static void test_seven_yields_plong(void)
{
TEST_IGNORE();
convert_drops(7, "Plong");
}
static void test_six_yields_pling(void)
{
TEST_IGNORE();
convert_drops(6, "Pling");
}
static void test_nine_yields_pling(void)
{
TEST_IGNORE();
convert_drops(9, "Pling");
}
static void test_ten_yields_plang(void)
{
TEST_IGNORE();
convert_drops(10, "Plang");
}
static void test_fourteen_yields_plong(void)
{
TEST_IGNORE();
convert_drops(14, "Plong");
}
static void test_fifteen_yields_plingplang(void)
{
TEST_IGNORE();
convert_drops(15, "PlingPlang");
}
static void test_twenty_one_yields_plingplong(void)
{
TEST_IGNORE();
convert_drops(21, "PlingPlong");
}
static void test_twenty_five_yields_plang(void)
{
TEST_IGNORE();
convert_drops(25, "Plang");
}
static void test_thirty_five_yields_plangplong(void)
{
TEST_IGNORE();
convert_drops(35, "PlangPlong");
}
static void test_forty_nine_yields_plong(void)
{
TEST_IGNORE();
convert_drops(49, "Plong");
}
static void test_fifty_two_yields_itself(void)
{
TEST_IGNORE();
convert_drops(52, "52");
}
static void test_one_hundred_five_yields_plingplangplong(void)
{
TEST_IGNORE();
convert_drops(105, "PlingPlangPlong");
}
static void test_big_prime_yields_itself(void)
{
TEST_IGNORE();
convert_drops(12121, "12121");
}
int main(void)
{
UnityBegin("raindrops.c");
RUN_TEST(test_one_yields_itself);
RUN_TEST(test_three_yields_pling);
RUN_TEST(test_five_yields_plang);
RUN_TEST(test_seven_yields_plong);
RUN_TEST(test_six_yields_pling);
RUN_TEST(test_nine_yields_pling);
RUN_TEST(test_ten_yields_plang);
RUN_TEST(test_fourteen_yields_plong);
RUN_TEST(test_fifteen_yields_plingplang);
RUN_TEST(test_twenty_one_yields_plingplong);
RUN_TEST(test_twenty_five_yields_plang);
RUN_TEST(test_thirty_five_yields_plangplong);
RUN_TEST(test_forty_nine_yields_plong);
RUN_TEST(test_fifty_two_yields_itself);
RUN_TEST(test_one_hundred_five_yields_plingplangplong);
RUN_TEST(test_big_prime_yields_itself);
return UnityEnd();
}
#include "stdio.h"
#include "string.h"
#include "raindrops.h"
void convert(char* result, int drops)
{
if (drops % 3 == 0) strcat(result, "Pling");
if (drops % 5 == 0) strcat(result, "Plang");
if (drops % 7 == 0) strcat(result, "Plong");
if (strlen(result) == 0) sprintf(result, "%d", drops);
}
#ifndef RAINDROPS_H
#define RAINDROPS_H
void convert(char* result, int drops);
#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,450 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