🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # vlzware's solution

## to Series in the C Track

Published at Jul 13 2018 · 1 comment
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.

Given a string of digits, output all the contiguous substrings of length `n` in that string.

For example, the string "49142" has the following 3-digit series:

• 491
• 914
• 142

And the following 4-digit series:

• 4914
• 9142

And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get.

Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.

## Getting Started

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.

## 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

A subset of the Problem 8 at Project Euler http://projecteuler.net/problem=8

## Submitting Incomplete Solutions

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

### test_series.c

``````#include <stdlib.h>
#include "vendor/unity.h"
#include "../src/series.h"

void setUp(void)
{
}

void tearDown(void)
{
}

// helper routines to validate test results
static void test_solution(series_results_t * expected_solution,
series_results_t * actual_solution)
{
// result count correct
TEST_ASSERT_EQUAL(expected_solution->substring_count,
actual_solution->substring_count);

// test each substring...
for (unsigned int index = 0; index < expected_solution->substring_count;
index++) {
TEST_ASSERT_EQUAL_STRING(expected_solution->substring[index],
actual_solution->substring[index]);
free(actual_solution->substring[index]);
}
if (actual_solution->substring_count) {
free(actual_solution->substring);
}
}

// Test cases
void test_slices_of_one(void)
{
char *substrings[] = { "0", "1", "2", "3", "4" };
series_results_t expected = { 5, &substrings };
series_results_t actual = series("01234", 1);

test_solution(&expected, &actual);
}

void test_slices_of_two(void)
{
TEST_IGNORE();               // delete this line to run test
char *substrings[] = { "97", "78", "86", "67", "75", "56", "64" };
series_results_t expected = { 7, &substrings };
series_results_t actual = series("97867564", 2);

test_solution(&expected, &actual);
}

void test_slices_of_three(void)
{
TEST_IGNORE();
char *substrings[] = { "978", "786", "867", "675", "756", "564" };
series_results_t expected = { 6, &substrings };
series_results_t actual = series("97867564", 3);

test_solution(&expected, &actual);
}

void test_slices_of_four(void)
{
TEST_IGNORE();
char *substrings[] = { "0123", "1234" };
series_results_t expected = { 2, &substrings };
series_results_t actual = series("01234", 4);

test_solution(&expected, &actual);
}

void test_slices_of_five(void)
{
TEST_IGNORE();
char *substrings[] = { "01234" };
series_results_t expected = { 1, &substrings };
series_results_t actual = series("01234", 5);

test_solution(&expected, &actual);
}

void test_overly_long_slice(void)
{
TEST_IGNORE();
char *substrings[] = { "" };
series_results_t expected = { 0, &substrings };
series_results_t actual = series("012", 4);

test_solution(&expected, &actual);
}

void test_overly_short_slice(void)
{
TEST_IGNORE();
char *substrings[] = { "" };
series_results_t expected = { 0, &substrings };
series_results_t actual = series("01234", 0);

test_solution(&expected, &actual);
}

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

RUN_TEST(test_slices_of_one);
RUN_TEST(test_slices_of_two);
RUN_TEST(test_slices_of_three);
RUN_TEST(test_slices_of_four);
RUN_TEST(test_slices_of_five);
RUN_TEST(test_overly_long_slice);
RUN_TEST(test_overly_short_slice);

UnityEnd();

return 0;
}``````

### src/series.c

``````#include "series.h"
#include <stdlib.h>
#include <string.h>

void check_alloc(void *p);

series_results_t series(char *input_text, unsigned int substring_length)
{
series_results_t res;
int len = (input_text == NULL)? 0 : strlen(input_text);
if (((int)substring_length > len) || (len == 0)
|| (substring_length == 0)) {
res.substring_count = 0;
return res;
}

res.substring_count = len - substring_length + 1;
res.substring = (char**) malloc(res.substring_count * sizeof(char*));
check_alloc(res.substring);

int i;
for (i = 0; i < (int)res.substring_count; i++) {
res.substring[i] = (char*) malloc(substring_length + 1);
check_alloc(res.substring[i]);
strncpy(res.substring[i], input_text, substring_length);
res.substring[i][substring_length] = '\0';
input_text++;
}

return res;
}

#include <stdio.h>
void check_alloc(void *p)
{
if (p == NULL) {
fprintf(stderr, "Memory error!\n");
exit(1);
}
}``````

### src/series.h

``````#ifndef SERIES_H
#define SERIES_H

#define MAX_INPUT_TEXT_LENGTH (8)
#define MAX_SERIES_RESULTS (MAX_INPUT_TEXT_LENGTH)
#define MAX_SERIES_LENGTH (5)

// results structure
typedef struct series_results {
unsigned int substring_count;
char **substring;            // array of pointers of dimension substring_count
} series_results_t;

// series - routine to split up input text into consecutive substrings of text of a given substringLength
//
// inputs:
//    inputText - bounded to MAX_INPUT_TEXT_LENGTH
//    substringLength - the desired length of substrings that are returned bounded to MAX_SERIES_LENGTH
//
// outputs:
//    SeriesResult_t results - structure containing count of the substring and an array of
//       pointers to the substrings.
//
//       calling routine will free memory associated with each of the substrings and
//       the pointers to the substrings.
series_results_t series(char *input_text, unsigned int substring_length);

#endif`````` Solution Author
commented over 2 years ago

Forgot that strlen does not check for NULL.

### 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?