How to Convert Arabic Numerals (Integers) to English Words (C/C++ Solution)?


Convert a non-negative integer to its English words representation. Given input is guaranteed to be less than 231 – 1.

For example,
123 – “One Hundred Twenty Three”
12345 – “Twelve Thousand Three Hundred Forty Five”
1234567 – “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”

Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? the middle chunk is zero and should not be printed out
Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.

IntegerToEnglish How to Convert Arabic Numerals (Integers) to English Words (C/C++ Solution)? c / c++ code code library coding exercise leetcode online judge math

IntegerToEnglish

C++ Solution

1. Some Corner cases, 0 to be ‘Zero’ and 1000010 to be ‘One Million Ten’ (middle zero should not be printed)
2. Should not print additional spaces.
3. You can use iterative or recursion to concatenate the intermediate results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Solution {
public:
    string
    LongToEnglish(unsigned long x) {
        switch(x) {
            case 0: return "Zero";
            case 1: return "One";
            case 2: return "Two";
            case 3: return "Three";
            case 4: return "Four";
            case 5: return "Five";
            case 6: return "Six";
            case 7: return "Seven";
            case 8: return "Eight";
            case 9: return "Nine";
            case 10: return "Ten";
            case 11: return "Eleven";
            case 12: return "Twelve";
            case 13: return "Thirteen";
            case 14: return "Fourteen";
            case 15: return "Fifteen";
            case 16: return "Sixteen";
            case 17: return "Seventeen";
            case 18: return "Eighteen";
            case 19: return "Nineteen";
            case 20: return "Twenty";
            case 30: return "Thirty";
            case 40: return "Forty";
            case 50: return "Fifty";
            case 60: return "Sixty";
            case 70: return "Seventy";
            case 80: return "Eighty";
            case 90: return "Ninety";
            case 100: return "One Hundred";
            case 1000: return "One Thousand";
            case 1000000: return "One Million";
            case 1000000000: return "One Billion";
        }
        // less than 100
        for (long i = 1; i <= 9; i ++) {
            long j = i * 10;
            if ((x >= j) && (x < j + 10)) {
                long r = x - j;
                return LongToEnglish(j) + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // less than 1000
        for (long i = 1; i <= 9; i ++) {
            long j = i * 100;
            if ((x >= j) && (x < j + 100)) {
                long r = x - j;
                return LongToEnglish(i) + " Hundred" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // less than 10000
        for (long i = 1; i <= 9; i ++) {
            long j = i * 1000;
            if ((x >= j) && (x < j + 1000)) {
                long r = x - j;
                return LongToEnglish(i) + " Thousand" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // Million
        for (long i = 1; i <= 9; i ++) {
            long j = i * 1000000;
            if ((x >= j) && (x < j + 1000000)) {
                long r = x - j;
                return LongToEnglish(i) + " Million" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // Billion
        for (long i = 1; i <= 4; i ++) {
            long j = i * 1000000000;
            if ((x >= j) && (x < j + 1000000000)) {
                long r = x - j;
                return LongToEnglish(i) + " Billion" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // Divide the number into 3-digit groups from left to right
        string output = "";
        long cnt = 0;
        while (x > 0) {
            long y = x % 1000;
            x /= 1000;
            if (y > 0) { // skip middle-chunk zero
                string t = "";
                if (cnt == 1) t = " Thousand ";
                if (cnt == 2) t = " Million ";
                if (cnt == 3) t = " Billion ";
                output = LongToEnglish(y) + t + output;
            }
            cnt ++;
        }
        if (output[output.length() - 1] == ' ')
        { // "Three Thousand " == > "Three Thousand"
            return output.substr(0, output.length() - 1);
        }
        return (output);
    }
    
    string numberToWords(int num) {
        return LongToEnglish(num);
    }
};
class Solution {
public:
    string
    LongToEnglish(unsigned long x) {
        switch(x) {
            case 0: return "Zero";
            case 1: return "One";
            case 2: return "Two";
            case 3: return "Three";
            case 4: return "Four";
            case 5: return "Five";
            case 6: return "Six";
            case 7: return "Seven";
            case 8: return "Eight";
            case 9: return "Nine";
            case 10: return "Ten";
            case 11: return "Eleven";
            case 12: return "Twelve";
            case 13: return "Thirteen";
            case 14: return "Fourteen";
            case 15: return "Fifteen";
            case 16: return "Sixteen";
            case 17: return "Seventeen";
            case 18: return "Eighteen";
            case 19: return "Nineteen";
            case 20: return "Twenty";
            case 30: return "Thirty";
            case 40: return "Forty";
            case 50: return "Fifty";
            case 60: return "Sixty";
            case 70: return "Seventy";
            case 80: return "Eighty";
            case 90: return "Ninety";
            case 100: return "One Hundred";
            case 1000: return "One Thousand";
            case 1000000: return "One Million";
            case 1000000000: return "One Billion";
        }
        // less than 100
        for (long i = 1; i <= 9; i ++) {
            long j = i * 10;
            if ((x >= j) && (x < j + 10)) {
                long r = x - j;
                return LongToEnglish(j) + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // less than 1000
        for (long i = 1; i <= 9; i ++) {
            long j = i * 100;
            if ((x >= j) && (x < j + 100)) {
                long r = x - j;
                return LongToEnglish(i) + " Hundred" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // less than 10000
        for (long i = 1; i <= 9; i ++) {
            long j = i * 1000;
            if ((x >= j) && (x < j + 1000)) {
                long r = x - j;
                return LongToEnglish(i) + " Thousand" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // Million
        for (long i = 1; i <= 9; i ++) {
            long j = i * 1000000;
            if ((x >= j) && (x < j + 1000000)) {
                long r = x - j;
                return LongToEnglish(i) + " Million" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // Billion
        for (long i = 1; i <= 4; i ++) {
            long j = i * 1000000000;
            if ((x >= j) && (x < j + 1000000000)) {
                long r = x - j;
                return LongToEnglish(i) + " Billion" + (r > 0 ? (" " + LongToEnglish(r)): "");
            }
        }
        // Divide the number into 3-digit groups from left to right
        string output = "";
        long cnt = 0;
        while (x > 0) {
            long y = x % 1000;
            x /= 1000;
            if (y > 0) { // skip middle-chunk zero
                string t = "";
                if (cnt == 1) t = " Thousand ";
                if (cnt == 2) t = " Million ";
                if (cnt == 3) t = " Billion ";
                output = LongToEnglish(y) + t + output;
            }
            cnt ++;
        }
        if (output[output.length() - 1] == ' ')
        { // "Three Thousand " == > "Three Thousand"
            return output.substr(0, output.length() - 1);
        }
        return (output);
    }
    
    string numberToWords(int num) {
        return LongToEnglish(num);
    }
};

The counting in English is easy to implement, as compared to other languages e.g. French or German. As the 4-byte unsigned integer can hold maxium 232-1 numbers, so it is possible to iterative each possibility, grouping them by number of zeros.

Online Converter with API

This online Arabic-Integer-to-English tool extends the integer ranges from negative 2^63-1 to 2^63-1 (that is from –9223372036854775807 to 9223372036854775807) using the 64-bit signed integer.

It also provides an easy-to-use (fair use policy) API to convert the Arabic Numerals (Integers) to English Words. The numbers are converted into short-scale representations where currently the UK and the American are using. i.e. 1 Billion equals 10^9 (nine zeros) and 1 Million equals 10^6 (six zeros).

NPM JS Library

I have made a NPM Js Library (MIT License): https://www.npmjs.com/package/int2english that supports negative integer (signed 64-bit).

The steemit post: NPM Js Library to Convert Integers to English

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
792 words
Last Post: Upgrading to Delphi 10 Seattle (XE10)
Next Post: Linux BASH Coding Exercise - Transpose File

The Permanent URL is: How to Convert Arabic Numerals (Integers) to English Words (C/C++ Solution)?

2 Comments

Leave a Reply