Avatar of paiv

paiv's solution

to Say in the C++ Track

Published at Sep 22 2019 · 0 comments
Test suite

Given a number from 0 to 999,999,999,999, spell out that number in English.

Step 1

Handle the basic case of 0 through 99.

If the input to the program is 22, then the output should be 'twenty-two'.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

  • 0
  • 14
  • 50
  • 98
  • -1
  • 100


If you're on a Mac, shell out to Mac OS X's say program to talk out loud. If you're on Linux or Windows, eSpeakNG may be available with the command espeak.

Step 2

Implement breaking a number up into chunks of thousands.

So 1234567890 should yield a list like 1, 234, 567, and 890, while the far simpler 1000 should yield just 1 and 0.

The program must also report any values that are out of range.

Step 3

Now handle inserting the appropriate scale word between those chunks.

So 1234567890 should yield '1 billion 234 million 567 thousand 890'

The program must also report any values that are out of range. It's fine to stop at "trillion".

Step 4

Put it all together to get nothing but plain English.

12345 should give twelve thousand three hundred forty-five.

The program must also report any values that are out of range.


Use and (correctly) when spelling out the number in English:

  • 14 becomes "fourteen".
  • 100 becomes "one hundred".
  • 120 becomes "one hundred and twenty".
  • 1002 becomes "one thousand and two".
  • 1323 becomes "one thousand three hundred and twenty-three".

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.


A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

Submitting Incomplete Solutions

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


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

    REQUIRE("zero" == say::in_english(0ULL));

    REQUIRE("one" == say::in_english(1ULL));

    REQUIRE("fourteen" == say::in_english(14ULL));

    REQUIRE("twenty" == say::in_english(20ULL));

    REQUIRE("twenty-two" == say::in_english(22ULL));

    REQUIRE("sixty-nine" == say::in_english(69ULL));

    REQUIRE("one hundred" == say::in_english(100ULL));

    REQUIRE("one hundred twenty-three" == say::in_english(123ULL));

    REQUIRE("one thousand" == say::in_english(1000ULL));

    REQUIRE("one thousand two hundred thirty-four" == say::in_english(1234ULL));

    REQUIRE("one million" == say::in_english(1000ULL*1000ULL));

    REQUIRE("one million two" == say::in_english(1000ULL*1000ULL + 2ULL));

    REQUIRE("one million two thousand three hundred forty-five" == say::in_english(1002345ULL));

    REQUIRE("one billion" == say::in_english(1000ULL*1000ULL*1000ULL));

        "nine hundred eighty-seven billion six hundred fifty-four million "
        "three hundred twenty-one thousand one hundred twenty-three" ==

    REQUIRE_THROWS_AS(say::in_english(-1), std::domain_error);

    REQUIRE_THROWS_AS(say::in_english(1000ULL * 1000ULL * 1000ULL * 1000ULL),


#include "say.h"
#include <functional>
#include <vector>

namespace say {

using std::function;
using std::vector;

static const string teen[] = {"zero",
    "one", "two", "three", "four", "five", "six", "seven", "eight",
    "nine", "ten", "elleven", "twelve", "thirteen", "fourteen", "fifteen",
    "sixteen", "seventeen", "eighteen", "nineteen"};

static const string nty[] = {"",
    "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
    "eighty", "ninety", "hundred"};

static const string illion[] = {"", "thousand", "million", "billion"};

static string
join(const vector<string>& v, const string& sep) {
    string so;
    for (auto it = v.begin(); it != v.end(); it++) {
        if (it > v.begin()) {
            so += sep;
        so += *it;
    return so;

string in_english(ill n) {
    if (n < 0 || n > 999999999999) {
        throw std::domain_error("some");

    if (n == 0) {
        return teen[0];

    vector<string> words;

    function<void(int)> inner = [&words, &inner] (int n) {
        if (n == 0) {

        if (n < 20) {

        if (n < 100) {
            if (n % 10) {
                words.push_back(string(nty[n / 10]) + "-" + teen[n % 10]);
            else {
                words.push_back(nty[n / 10]);

        words.push_back(teen[n / 100]);
        inner(n % 100);

    vector<int> parts;
    while (n > 0) {
        parts.push_back(n % 1000);
        n /= 1000;

    for (auto it = parts.rbegin(); it != parts.rend(); it++) {
        int x = *it;
        auto m = parts.rend() - it;
        if (m > 1 && x > 0) {
            words.push_back(illion[m - 1]);

    return join(words, " ");

}  // namespace say


#if !defined(SAY_H)
#define SAY_H

#include <string>

namespace say {

typedef long long ill;
using std::string;

string in_english(ill n);

}  // namespace say

#endif // SAY_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?