Two-fer
or 2-fer
is short for two for one. One for you and one for me.
Given a name, return a string with the message:
One for X, one for me.
Where X is the given name.
However, if the name is missing, return the string:
One for you, one for me.
Here are some examples:
Name | String to return |
---|---|
Alice | One for Alice, one for me. |
Bob | One for Bob, one for me. |
One for you, one for me. | |
Zaphod | One for Zaphod, one for me. |
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.
https://github.com/exercism/problem-specifications/issues/757
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
#include "vendor/unity.h"
#include "../src/two_fer.h"
#include <stddef.h>
#define BUFFER_SIZE (100)
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_two_fer_no_name_given(void)
{
char response[BUFFER_SIZE];
const char *expected = "One for you, one for me.";
two_fer(response, NULL);
TEST_ASSERT_EQUAL_STRING(expected, response);
}
static void test_two_fer_a_name_given(void)
{
TEST_IGNORE(); // delete this line to run test
char response[BUFFER_SIZE];
const char *expected = "One for Alice, one for me.";
two_fer(response, "Alice");
TEST_ASSERT_EQUAL_STRING(expected, response);
}
static void test_two_fer_another_name_given(void)
{
TEST_IGNORE();
char response[BUFFER_SIZE];
const char *expected = "One for Bob, one for me.";
two_fer(response, "Bob");
TEST_ASSERT_EQUAL_STRING(expected, response);
}
int main(void)
{
UnityBegin("test/test_two_fer.c");
RUN_TEST(test_two_fer_no_name_given);
RUN_TEST(test_two_fer_a_name_given);
RUN_TEST(test_two_fer_another_name_given);
return UnityEnd();
}
#include "string.h"
#include "two_fer.h"
void two_fer(char* response, char* name)
{
response[0] = '\0';
strcat(response, "One for ");
if (name == NULL) strcat(response, "you");
else strcat(response, name);
strcat(response, ", one for me.");
}
#ifndef TWO_FER_H
#define TWO_FER_H
void two_fer(char* response, char* name);
#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