Avatar of matheussilvasantos

matheussilvasantos's solution

to Acronym 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.

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

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

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);
   UnityEnd();
   return 0;
}
#include "acronym.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

bool is_separator_chr(char chr) {
  return chr == ' ' || chr == '-';
}

char *abbreviate(const char *phrase) {
  if(!phrase) return NULL;

  char *acronym = NULL;
  int phrase_length = strlen(phrase);

  if(phrase_length <= 1) return acronym;

  acronym = (char *) realloc(acronym, sizeof(char)); 
  *acronym = *(phrase++);

  int acronym_length = 1;

  while(*(phrase++)) {
    if(is_separator_chr(*phrase)) {
      acronym = (char *) realloc(acronym, sizeof(char)); 

      char letter = toupper(*(phrase + 1));
      *(acronym + acronym_length++) = letter;
    }
  }

  return acronym;
}

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?