Home Arrays Convert a number into a string of words

# Convert a number into a string of words

Question: Given an integer N, convert it into a string of words.

Input: N = 345
Output: three hundred forty five

This is one of the most common problems that we come across in out daily lives. We need to input a number from the user and print it in words.

First, we perform a number of checks on the input string. If it is in any way invalid, the result is the empty string (“”).

Next, we take note of whether or not the string begins with a ‘-‘. If it does we remove it.

If what is left is composed entirely of zeros, we just return “zero”.
The number can then be processed taking each digit at a time and evaluating it.
I have added comments in the code for a better understanding of the algorithm.

.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
/* Program to print a given number in words. The program handles
numbers from 0 to 9999 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

/* A function that prints given number in words */
void convert_to_words(char *num)
{
int len = strlen(num);  // Get number of digits in given number

/* Base cases */
if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported\n");
return;
}

/* The first string is not used, it is to make array indexing simple */
string single_digits[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};

/* The first string is not used, it is to make array indexing simple */
string two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

/* The first two string are not used, they are to make array indexing simple*/
string tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};

string tens_power[] = {"hundred", "thousand"};

/* Used for debugging purpose only */
printf("\n%s: ", num);

/* For single digit number */
if (len == 1) {
cout << single_digits[*num - '0'] << " ";
return;
}

/* Iterate while num is not '\0' */
while (*num != '\0') {

/* Code path for first 2 digits */
if (len >= 3) {
if (*num -'0' != 0) {
cout << single_digits[*num - '0'] << " ";
cout << tens_power[len-3] << " "; // here len can be 3 or 4
}
--len;
}

/* Code path for last 2 digits */
else {
/* Need to explicitly handle 10-19. Sum of the two digits is
used as index of "two_digits" array of strings */
if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
cout << two_digits[sum] << " ";
return;
}

/* Need to explicitely handle 20 */
else if (*num == '2' && *(num + 1) == '0') {
cout << "twenty ";
return;
}

/* Rest of the two digit numbers i.e., 21 to 99 */
else {
int i = *num - '0';
cout << (i? tens_multiple[i]: "") << " ";
++num;
if (*num != '0')
cout << single_digits[*num - '0'] << " ";
}
}
++num;
}
}

/* Driver program to test above function */
int main(void)
{
cout << "Enter a number to convert to words:- ";

char num;

cin >> num;
convert_to_words(num);

cout << endl;

return 0;
}
Code language: C++ (cpp)

Here is the running link of the code:- http://ideone.com/p6aUvz

#### You may also like September 7, 2016 - 16:36

Thanks. April 7, 2016 - 13:57

You’re code is great. I have one doubt. There is no check being done for signed number or invalid input string. Why? May 27, 2016 - 10:48

Hi Akilan,

I understand your doubt, but this is not a homework question that takes care of invalid input. The problem concentrates specifically on the logic of the problem. January 11, 2016 - 12:51

Thanks nikoo28. You r the best! December 17, 2015 - 17:28

can u explain the logic? I understood when the string contains only single character.