Avatar of thekeele

thekeele's solution

to Two Fer in the C Track

Published at Jul 30 2019 · 0 comments
Instructions
Test suite
Solution

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.

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

https://github.com/exercism/problem-specifications/issues/757

Submitting Incomplete Solutions

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

test_two_fer.c

#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();
}

src/two_fer.c

#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.");
}

src/two_fer.h

#ifndef TWO_FER_H
#define TWO_FER_H

void two_fer(char* response, char* name);

#endif

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?