How to convert numeric words to numbers in C++ 20

Converting numeric words to their corresponding numerical values can be effortlessly achieved using the numword class. This class offers a convenient way to transform written-out numbers into their numeric forms, making tasks like data processing, language translation, and text-to-number conversions more streamlined. This can be especially useful in scenarios where textual number representations need to be processed as actual numerical data, simplifying computations and enhancing the efficiency of various applications.

Solution approach

We currently hold three files that encompass the entire solution. Provided below are the structure and detailed explanation of these files:

The numword.h file

This file forms the foundation of the numword class, establishing the essential elements required for word-to-number conversion. Within the bw namespace, it defines aliases like string, string_view, and numnum (uint64_t) for enhanced code clarity. The declaration of wordToNumberMap sets the stage for translating words to numeric values. The class resides within the bw namespace and encompasses various members and methods, facilitating its core functionality. The header provides a comprehensive structure that enables efficient and accurate word-to-number conversion through the numword class.

#ifndef _NUMWORD_H
#define _NUMWORD_H
#include <string>
#include <vector>
#include <unordered_map>
namespace bw {
using std::string;
using std::string_view;
using numnum = uint64_t;
// Define your word to number mapping here
extern const std::unordered_map<string_view, numnum> wordToNumberMap;
class numword {
// ... (other members and methods)
public:
// ... (other constructors and methods)
// Convert words to numbers
numnum operator() (const string& words);
numnum convertMultiWord(const std::vector<string_view>& words);
};
}
#endif

The numword-test.cpp file

This file is dedicated to testing the numword class. It includes a formatter specialization that enables easy use of the format() function with bw::numword objects. Additionally, a custom print() function bypasses cout, simplifying the output process.

#include <fmt/format.h>
#include "numword.h"
using fmt::format;
int main() {
bw::numword nw{};
fmt::print("Number for '{}' is {}\n", "three", nw("three"));
fmt::print("Number for '{}' is {}\n", "forty-seven", nw("forty seven"));
fmt::print("Number for '{}' is {}\n", "seventy-three", nw("seventy three"));
fmt::print("Number for '{}' is {}\n", "eighteen", nw("eighteen"));
fmt::print("Number for '{}' is {}\n", "twenty-five", nw("twenty five"));
fmt::print("Number for '{}' is {}\n", "one hundred fifty-seven", nw("one hundred fifty seven"));
fmt::print("Number for '{}' is {}\n", "nine hundred", nw("nine hundred"));
return 0;
}

The numword.cpp file

This implementation file handles the core logic of the numword class. It consists of private methods for manipulating the output buffer, calculating powers, and the crucial words() function. The words() function is where the magic happens. It systematically dissects a number into its components (such as hundreds, tens, etc.) and assembles the corresponding words from arrays of string_view constants.

#include <fmt/format.h>
#include "numword.h"
using fmt::format;

int main() {
    bw::numword nw{};

    fmt::print("Number for '{}' is {}\n", "three", nw("three"));
    fmt::print("Number for '{}' is {}\n", "forty-seven", nw("forty seven"));
    fmt::print("Number for '{}' is {}\n", "seventy-three", nw("seventy three"));
    fmt::print("Number for '{}' is {}\n", "eighteen", nw("eighteen"));
    fmt::print("Number for '{}' is {}\n", "twenty-five", nw("twenty five"));
    fmt::print("Number for '{}' is {}\n", "one hundred fifty-seven", nw("one hundred fifty seven"));
    fmt::print("Number for '{}' is {}\n", "nine hundred", nw("nine hundred"));
    
    return 0;
}
Converting words to number

Code explanation

The following is a line-by-line explanation of code in numword.cpp.

  • Lines 1–3: Include necessary headers for the code to work.

  • Lines 5–8: Begin defining the namespace bw and set up the mapping of words to their corresponding numeric values.

  • Lines 8–42: Define the numword::operator() function, which takes a string of words and converts it to a numeric value.

  • Lines 44–49: Initialize variables and containers used in the conversion process.

  • Lines 51–58: Loop through each word in the input string and handle cases such as skipping "and" handling negative indicators, and looking up words in the mapping.

  • Lines 59–66: Searches for a given word in a map named wordToNumberMap. If the word is found:

    • If the corresponding numeric value (it->second) is 100 or greater, the currentNumber is multiplied by that value.

    • If the numeric value is less than 100, the currentNumber is increased by that value.

  • Lines 67–71: If the current numeric value is in the thousands or above, add it to the result and reset the current value.

  • Lines 73–76: Add the remaining current numeric value to the result. If the input contained negative indicators, negate the result.

  • Line 77: Return the final calculated numeric value.

This code defines a function that can convert a string of words representing numbers into their corresponding numeric values based on the provided mapping.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved