Avatar of shmibs

shmibs's solution

to Acronym in the C Track

Published at Nov 10 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

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

Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

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

test_acronym.c

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

void setUp(void)
{
}

void tearDown(void)
{
}

void test_abbreviation(char *phrase, char *expected)
{
   char *actual = abbreviate(phrase);
   TEST_ASSERT_EQUAL_STRING(expected, actual);
   free(actual);
}

void test_null_string(void)
{
   TEST_IGNORE();               // delete this line to run test
   char *phrase = NULL;
   char *expected = NULL;
   test_abbreviation(phrase, expected);
}

void test_basic_abbreviation(void)
{
   TEST_IGNORE();
   char *phrase = "Portable Network Graphics";
   char *expected = "PNG";
   test_abbreviation(phrase, expected);
}

void test_lower_case_words(void)
{
   TEST_IGNORE();
   char *phrase = "Ruby on Rails";
   char *expected = "ROR";
   test_abbreviation(phrase, expected);
}

void test_punctuation(void)
{
   TEST_IGNORE();
   char *phrase = "First In, First Out";
   char *expected = "FIFO";
   test_abbreviation(phrase, expected);
}

void test_non_acronym_all_caps_words(void)
{
   TEST_IGNORE();
   char *phrase = "GNU Image Manipulation Program";
   char *expected = "GIMP";
   test_abbreviation(phrase, expected);
}

void test_hyphenated(void)
{
   TEST_IGNORE();
   char *phrase = "Complementary metal-oxide semiconductor";
   char *expected = "CMOS";
   test_abbreviation(phrase, expected);
}

void test_all_caps_words(void)
{
   TEST_IGNORE();
   char *phrase = "PHP: Hypertext Preprocessor";
   char *expected = "PHP";
   test_abbreviation(phrase, expected);
}

void test_empty_string(void)
{
   TEST_IGNORE();
   char *phrase = "";
   char *expected = NULL;
   test_abbreviation(phrase, expected);
}

void test_all_words_starting_with_lowercase(void)
{
   TEST_IGNORE();
   char *phrase = "for what it's worth";
   char *expected = "FWIW";
   test_abbreviation(phrase, expected);
}

void test_long_abbreviation(void)
{
   TEST_IGNORE();
   char *phrase = "Rolling On The Floor Laughing So Hard "
       "That My Dogs Came Over And Licked Me";
   char *expected = "ROTFLSHTMDCOALM";
   test_abbreviation(phrase, expected);
}

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

   RUN_TEST(test_basic_abbreviation);
   RUN_TEST(test_null_string);
   RUN_TEST(test_lower_case_words);
   RUN_TEST(test_punctuation);
   RUN_TEST(test_non_acronym_all_caps_words);
   RUN_TEST(test_hyphenated);
   RUN_TEST(test_all_caps_words);
   RUN_TEST(test_empty_string);
   RUN_TEST(test_all_words_starting_with_lowercase);
   RUN_TEST(test_long_abbreviation);
   UnityEnd();
   return 0;
}
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <locale.h>
#include <ctype.h>

char *abbreviate(const char *phrase) {
	bool pass, inc;
	uint64_t i, c, o;
	char *r;

	/* yeh no miss me withat */
	if (phrase == NULL || phrase[0] == '\0')
		return NULL;

	/* two pass, for a single alloc */
	for (pass = 0, c = 1, inc = true, o = 0;; inc = true) {
		for (i = 0; phrase[i] != '\0'; i++) {
			/* FUTURE PROOF™, just in case someone's passing in
			 * exabytes of data */
			if (i == UINT64_MAX)
				return NULL;

			if (pass == 0) {
				/* calculate length */
				if (inc && isalpha(phrase[i])) {
					inc = false;
					c++;
				} else if (isspace(phrase[i]) || phrase[i] == '-') {
					inc = true;
				}
			} else {
				/* build acronym */
				if (inc && isalpha(phrase[i])) {
					inc = false;
					r[o] = toupper(phrase[i]);
					o++;
				} else if (isspace(phrase[i]) || phrase[i] == '-') {
					inc = true;
				}

			}
		}

		if (pass)
			break;
		else
			pass = 1;

		r = calloc(c, sizeof(char));
		if (r == NULL)
			return NULL;
	}

	return r;
}

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?