Avatar of xxylem

xxylem's solution

to Acronym in the C++ Track

Published at Oct 20 2019 · 0 comments
Instructions
Test suite
Solution

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.

#if defined(EXERCISM_RUN_ALL_TESTS)

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++14 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.

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.

acronym_test.cpp

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

using namespace std;

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

    const string expected{"PNG"};

    REQUIRE(expected == actual);
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("lowercase_words")
{
    const string actual = acronym::acronym("Ruby on Rails");

    const string expected{"ROR"};

    REQUIRE(expected == actual);
}

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

    const string expected{"FIFO"};

    REQUIRE(expected == actual);
}

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

    const string expected{"PHP"};

    REQUIRE(expected == actual);
}

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

    const string expected{"GIMP"};

    REQUIRE(expected == actual);
}

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

    const string expected{"CMOS"};

    REQUIRE(expected == actual);
}

#endif

acronym.cpp

#include "acronym.h"

namespace acronym {

	using namespace std;

	const string acronym(const string& long_name)
	{
		string abbreviated_name;
		abbreviated_name.reserve(4); // Assume most acronyms are 3-4 letters.
		abbreviated_name += long_name.front();
		const int n = long_name.size() - 1;

		for (int i = 1; i < n; i++)
		{
			const char& previous_char = long_name.at(i - 1);
			const char& current_char = long_name.at(i);

			if (!isalpha(previous_char))
				if (isalpha(current_char))
					abbreviated_name += toupper(current_char);
		}

		return abbreviated_name;
	}

}  // namespace acronym

acronym.h

#if !defined(ACRONYM_H)
#define ACRONYM_H

#include <string>

namespace acronym {

	const std::string acronym(const std::string& long_name);

}  // namespace acronym

#endif // ACRONYM_H

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?