Software & Finance





Turbo C++ - Convert Numbers to Text (Words)





Here is the Turbo C++ program for converting numbers into words. The logic here is to reuse the same string as much as possible. If you look at the string array in the code, I have defined strones and strtens. Once we have that, we can parse the number and get the corresponding string and appending it to come up with the result.


I have given here the code to display the numbers upto 100,000. If you want more limit, you can keep extending the program. 

 

Here are other useful links where you can find the same program in other programming languages:

 



Source Code


#include <iostream.h>

#include <stdio.h>

#include <string.h> 

 

bool HelperConvertNumberToText(int num, char *buf, int len)

{

    static char *strones[] = {

      "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",

      "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",

      "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",

    };

 

    static char *strtens[] = {

        "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",

        "Seventy", "Eighty", "Ninety", "Hundred"

    };

 

    char result[1024]; 

    int single, tens, hundreds;

 

    if(num > 1000)

        return false;

 

    hundreds = num / 100;

    num = num - hundreds * 100;

    if( num < 20)

    {

        tens = 0; // special case

        single = num;

    }

    else

    {

        tens = num / 10;

        num = num - tens * 10;

        single = num;

    }

 

    memset(result, 0, 1024);

   

    if(hundreds > 0)

    {

        strcat(result, strones[hundreds-1]);

        strcat(result, " Hundred ");

    }

    if(tens > 0)

    {

        strcat(result, strtens[tens-1]);

        strcat(result, " ");

    }

    if(single > 0)

    {

        strcat(result, strones[single-1]);

        strcat(result, " ");

    }

 

    if(len > strlen(result))

        strcpy(buf, result);

 

    return true;

}

 

bool ConvertNumberToText(int num, char *buf, int len)

{

    char tres[1024];

    char result[1024];

    int thousands;

    int temp;

    if(num < 0 || num > 100000)

    {

        printf( " %d \t Not Supported\n", num);

        return false;

    }

 

    if( num == 0)

    {

        printf(" %d \t Zero\n", num);

        return false;

    }

 

    memset(result, 0, 1024);

 

    if(num < 1000)

    {  

        HelperConvertNumberToText(num, (char*) &tres, 1024);

        strcat(result, tres);

    }

    else

    {

        thousands = num / 1000;

        temp = num - thousands * 1000;

 

        HelperConvertNumberToText(thousands, (char*) &tres, 1024);

        strcat(result, tres);

        strcat(result, "Thousand ");

        HelperConvertNumberToText(temp, (char*) &tres, 1024);

        strcat(result, tres);

    }

   

    if(len > strlen(result))

        strcpy(buf, result);

    return true;

}

 

int main()

{

    int len = 1024;

    char result[1024];

    int  i, num;

    static int arrNum[] =

    {

      -1, 0, 5, 10, 15, 19, 20, 21, 25, 33, 49, 50, 72,

      99, 100, 101, 117, 199, 200, 214, 517, 589, 999,

      1000, 1010, 1018, 1200, 9890, 10119, 13535, 57019,

      99999, 100000, 100001

    };

 

    for(i = 0; i < sizeof(arrNum) / sizeof(int); i++)

    {

        num = arrNum[i];

        if( ConvertNumberToText(num, result, len) == true)

           cout << num << "\t" << result << "\n";

    }

    return 0;

}

 

Output


-1       Not Supported

0        Zero

5        Five

10       Ten

15       Fifteen

19       Nineteen

20       Twenty

21       Twenty One

25       Twenty Five

33       Thirty Three

49       Fourty Nine

50       Fifty

72       Seventy Two

99       Ninety Nine

100      One Hundred

101      One Hundred One

117      One Hundred Seventeen

199      One Hundred Ninety Nine

200      Two Hundred

214      Two Hundred Fourteen

517      Five Hundred Seventeen

589      Five Hundred Eighty Nine

999      Nine Hundred Ninety Nine

1000     One Thousand

1010     One Thousand Ten

1018     One Thousand Eighteen

1200     One Thousand Two Hundred

9890     Nine Thousand Eight Hundred Ninety

10119    Ten Thousand One Hundred Nineteen

13535    Thirteen Thousand Five Hundred Thirty Five

57019    Fifty Seven Thousand Nineteen

99999    Ninety Nine Thousand Nine Hundred Ninety Nine

100000   One Hundred Thousand

100001   Not Supported