/* 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[10];
cin >> num;
convert_to_words(num);
cout << endl;
return 0;
}
LyogUHJvZ3JhbSB0byBwcmludC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ICAgIGNvdXQgPDwgc2luZ2xlX2RpZ2l0c1sqbnVtIC0gJzAnXSA8PCAiICI7CiAgICAgICAgICAgICAgICBjb3V0IDw8IHRlbnNfcG93ZXJbbGVuLTNdIDw8ICIgIjsgLy8gaGVyZSBsZW4gY2FuIGJlIDMgb3IgNAogICAgICAgICAgICB9CiAgICAgICAgICAgIC0tbGVuOwogICAgICAgIH0KCiAgICAgICAgLyogQ29kZSBwYXRoIGZvciBsYXN0IDIgZGlnaXRzICovCiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIC8qIE5lZWQgdG8gZXhwbGljaXRseSBoYW5kbGUgMTAtMTkuIFN1bSBvZiB0aGUgdHdvIGRpZ2l0cyBpcwogICAgICAgICAgICAgICB1c2VkIGFzIGluZGV4IG9mICJ0d29fZGlnaXRzIiBhcnJheSBvZiBzdHJpbmdzICovCiAgICAgICAgICAgIGlmICgqbnVtID09ICcxJykgewogICAgICAgICAgICAgICAgaW50IHN1bSA9ICpudW0gLSAnMCcgKyAqKG51bSArIDEpLSAnMCc7CiAgICAgICAgICAgICAgICBjb3V0IDw8IHR3b19kaWdpdHNbc3VtXSA8PCAiICI7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIC8qIE5lZWQgdG8gZXhwbGljaXRlbHkgaGFuZGxlIDIwICovCiAgICAgICAgICAgIGVsc2UgaWYgKCpudW0gPT0gJzInICYmICoobnVtICsgMSkgPT0gJzAnKSB7CiAgICAgICAgICAgICAgICBjb3V0IDw8ICJ0d2VudHkgIjsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgLyogUmVzdCBvZiB0aGUgdHdvIGRpZ2l0IG51bWJlcnMgaS5lLiwgMjEgdG8gOTkgKi8KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBpbnQgaSA9ICpudW0gLSAnMCc7CiAgICAgICAgICAgICAgICBjb3V0IDw8IChpPyB0ZW5zX211bHRpcGxlW2ldOiAiIikgPDwgIiAiOwogICAgICAgICAgICAgICAgKytudW07CiAgICAgICAgICAgICAgICBpZiAoKm51bSAhPSAnMCcpCiAgICAgICAgICAgICAgICAgICAgY291dCA8PCBzaW5nbGVfZGlnaXRzWypudW0gLSAnMCddIDw8ICIgIjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICArK251bTsKICAgIH0KfQoKLyogRHJpdmVyIHByb2dyYW0gdG8gdGVzdCBhYm92ZSBmdW5jdGlvbiAqLwppbnQgbWFpbih2b2lkKQp7CiAgICBjb3V0IDw8ICJFbnRlciBhIG51bWJlciB0byBjb252ZXJ0IHRvIHdvcmRzOi0gIjsKCiAgICBjaGFyIG51bVsxMF07CgogICAgY2luID4+IG51bTsKICAgIGNvbnZlcnRfdG9fd29yZHMobnVtKTsKCiAgICBjb3V0IDw8IGVuZGw7CgogICAgcmV0dXJuIDA7Cn0=