🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of vlzware

vlzware's solution

to Phone Number in the C Track

Published at Jul 13 2018 · 0 comments
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.

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: 1.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as

(NXX)-NXX-XXXX

where N is any digit from 2 through 9 and X is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

  • +1 (613)-995-0253
  • 613-995-0253
  • 1 613 995 0253
  • 613.995.0253

should all produce the output

6139950253

Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.

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

Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

Submitting Incomplete Solutions

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

test_phone_number.c

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

static char *result = NULL;

void setUp(void)
{
}

void tearDown(void)
{
   free(result);
   result = NULL;
}

void test_cleans_parens_dashes_and_spaces_from_the_number(void)
{
   const char input[] = "(123) 456-7890";
   const char expected[] = "1234567890";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_cleans_numbers_with_dots(void)
{
   TEST_IGNORE();               // delete this line to run test
   const char input[] = "123.456.7890";
   const char expected[] = "1234567890";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_cleans_numbers_with_multiple_spaces(void)
{
   TEST_IGNORE();
   const char input[] = "123 456   7890  ";
   const char expected[] = "1234567890";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_invalid_when_9_digits(void)
{
   TEST_IGNORE();
   const char input[] = "123456789";
   const char expected[] = "0000000000";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_valid_when_11_digits_and_first_digit_is_1(void)
{
   TEST_IGNORE();
   const char input[] = "11234567890";
   const char expected[] = "1234567890";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_invalid_when_11_digits_and_first_digit_not_1(void)
{
   TEST_IGNORE();
   const char input[] = "21234567890";
   const char expected[] = "0000000000";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_invalid_when_more_than_11_digits(void)
{
   TEST_IGNORE();
   const char input[] = "121234567890";
   const char expected[] = "0000000000";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_invalid_with_letters(void)
{
   TEST_IGNORE();
   const char input[] = "123-abc-7890";
   const char expected[] = "0000000000";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_invalid_with_punctuations(void)
{
   TEST_IGNORE();
   const char input[] = "123-@:!-7890";
   const char expected[] = "0000000000";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_invalid_with_right_number_of_digits_but_letters_mixed_in(void)
{
   TEST_IGNORE();
   const char input[] = "1a2b3c4d5e6f7g8h9i0j";
   const char expected[] = "0000000000";

   result = phone_number_clean(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_returns_area_code(void)
{
   TEST_IGNORE();
   const char input[] = "5024567890";
   const char expected[] = "502";

   result = phone_number_get_area_code(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_formats_a_number(void)
{
   TEST_IGNORE();
   const char input[] = "1234567890";
   const char expected[] = "(123) 456-7890";

   result = phone_number_format(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

void test_cleans_number_before_formatting(void)
{
   TEST_IGNORE();
   const char input[] = "123-456-7890";
   const char expected[] = "(123) 456-7890";

   result = phone_number_format(input);

   TEST_ASSERT_EQUAL_STRING(expected, result);
}

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

   RUN_TEST(test_cleans_parens_dashes_and_spaces_from_the_number);
   RUN_TEST(test_cleans_numbers_with_dots);
   RUN_TEST(test_cleans_numbers_with_multiple_spaces);
   RUN_TEST(test_invalid_when_9_digits);
   RUN_TEST(test_valid_when_11_digits_and_first_digit_is_1);
   RUN_TEST(test_invalid_when_11_digits_and_first_digit_not_1);
   RUN_TEST(test_invalid_when_more_than_11_digits);
   RUN_TEST(test_invalid_with_letters);
   RUN_TEST(test_invalid_with_punctuations);
   RUN_TEST(test_invalid_with_right_number_of_digits_but_letters_mixed_in);
   RUN_TEST(test_returns_area_code);
   RUN_TEST(test_formats_a_number);
   RUN_TEST(test_cleans_number_before_formatting);

   UnityEnd();
   return 0;
}

src/phone_number.c

#include "phone_number.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int check_str(const char *s);
char *err(char *res);
int is_allowed(char c);
void cpcnt(char **dst, char **src, int count);
void consume_space(char **s);
int peek(int count, int (*check) (int), char*s);

#define NUMLEN 10

char *phone_number_clean(const char *input)
{
	int len;
	if ((len = check_str(input)) < 0)
		return NULL;

	char *res = (char*) malloc(NUMLEN + 1);
	if (res == NULL)
		return NULL;

	char *tmp_r = res;
	char *tmp_i = (char*) input;
	int digits = 0;

	consume_space(&tmp_i);

	/* '+1 ' */
	if (*tmp_i == '+') {
		if (!(*(tmp_i + 1) == '1' && *(tmp_i + 2) == ' '))
			return err(res);
		tmp_i += 3;
		digits++;
	}

	/* '(ddd)' */
	if (*tmp_i == '(') {
		if (!peek(3, isdigit, tmp_i + 1) && *(tmp_i + 4) != ')')
			return err(res);
		tmp_i++;
		cpcnt(&tmp_r, &tmp_i, 3);
		tmp_i++;
		digits += 3;
	}

	consume_space(&tmp_i);

	if (*tmp_i == '1' && peek(NUMLEN, isdigit, tmp_i + 1))
		tmp_i++;

	while (*tmp_i) {
		if (isdigit(*tmp_i)) {
			if (++digits > NUMLEN)
				return err(res);
			*tmp_r = *tmp_i;
			tmp_r++;
		} else if (!is_allowed(*tmp_i)) {
			return err(res);
		}
		tmp_i++;
	}
	*tmp_r = '\0';

	if (digits != NUMLEN)
		return err(res);

	return res;
}

char *phone_number_get_area_code(const char *input)
{
	char *cl = phone_number_clean(input);
	char *res = (char*) malloc(4);
	if (res == NULL) {
		free(cl);
		return NULL;
	}

	res[0] = cl[0];
	res[1] = cl[1];
	res[2] = cl[2];
	res[3] = '\0';

	free(cl);

	return res;
}

char *phone_number_format(const char *input)
{
	char *cl = phone_number_clean(input);
	if (*cl == '0')
		return cl;

	char *res = (char*) malloc(15);
	if (res == NULL) {
		free(cl);
		return NULL;
	}

	int i;
	res[0] = '(';
	for (i = 0; i < 3; i++)
		res[1 + i] = cl[i];
	res[4] = ')';
	res[5] = ' ';
	for (i = 0; i < 3; i++)
		res[6 + i] = cl[3 + i];
	res[9] = '-';
	for (i = 0; i < 4; i++)
		res[10 + i] = cl[6 + i];
	res[14] = '\0';

	free(cl);
	return res;
}


/**
 ********************************************
 * helpers
 ********************************************
 */

int check_str(const char *s)
{
	if ((s == NULL) || (*s == '\0'))
		return -1;
	return strlen(s);
}

char *err(char *err)
{
	char *res = err;
	int i;
	for (i = 0; i < NUMLEN; i++)
		*err++ = '0';
	*err = '\0';
	return res;
}

int is_allowed(char c)
{
	return (c == ' ' || c == '.' || c == '-');
}

/* copy count chars skipping space */
void cpcnt(char **dst, char **src, int count)
{
	int i;
	for (i = 0; i < count; i++) {
		while (is_allowed(*(*src)))
			(*src)++;
		*(*dst)++ = *(*src)++;
	}
}

void consume_space(char **s)
{
	while (*(*s) == ' ')
		(*s)++;
}

int peek(int count, int (*check) (int), char*s)
{
	int i;
	for (i = 0; (i < count) && (*s); i++) {
		while(is_allowed(*s))
			s++;
		if (!check(*s++))
			return 0;
	}
	if (i < count)
		return 0;
	return 1;
}

src/phone_number.h

#ifndef PHONE_NUM_H
#define PHONE_NUM_H

char *phone_number_clean(const char *input);
char *phone_number_get_area_code(const char *input);
char *phone_number_format(const char *input);

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