Avatar of Akshive

Akshive's solution

to Acronym in the C++ Track

Published at Sep 29 2019 · 0 comments
Test suite

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 Installing and Running the Tests pages for C++ on exercism.io. 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. Create just enough structure by declaring namespaces, functions, classes, etc., 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, uncomment the next test by moving the following line past the next test.


This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++11 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.


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.


#include "acronym.h"
#include "test/catch.hpp"

using namespace std;

    const string actual = acronym::acronym("Portable Network Graphics");

    const string expected{"PNG"};

    REQUIRE(expected == actual);

    const string actual = acronym::acronym("Ruby on Rails");

    const string expected{"ROR"};

    REQUIRE(expected == actual);

    const string actual = acronym::acronym("First In, First Out");

    const string expected{"FIFO"};

    REQUIRE(expected == actual);

    const string actual = acronym::acronym("PHP: Hypertext Preprocessor");

    const string expected{"PHP"};

    REQUIRE(expected == actual);

    const string actual = acronym::acronym("GNU Image Manipulation Program");

    const string expected{"GIMP"};

    REQUIRE(expected == actual);

    const string actual = acronym::acronym("Complementary metal-oxide semiconductor");

    const string expected{"CMOS"};

    REQUIRE(expected == actual);

#include "acronym.h"
#include <string>
#include <regex>
namespace acronym {
    std::string acronym(std::string str)
        std::regex reg("[a-zA-Z][a-zA-Z:,]*");
        std::sregex_iterator currentMatch(str.begin(), str.end(), reg);
        std::sregex_iterator lastMatch;
        std::string ans = "";
        while(currentMatch != lastMatch){
            std::smatch match = *currentMatch;
        return ans;
}  // namespace 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?