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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

namespace m_n_space_ //i've put the functions inside a name space so you can use only the word_of() function.
{
//you can put your (#include)s here if you want to use the standard library functions instead of mine.
const char singles [10][20] = {"\0","one \0","two ","three ","four ","five ","six ","seven ","eight ","nine "};
const char teens [9] [20] = {"elevin ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "};
const char tens [9] [20] = {"ten ","twenty ","thirty ","fourty ","fifty ","sixty ","seventy ","eighty ","ninty "};
const char larges [5] [20] = {"hundred ","thousand ","million ","billion "};
const char place_singles [10][20] = {"first ","second ","third ","fourth ","fifth ","sixth ","seventh ","eighth ","ninth ","tenth "};
const char place_teens [9] [20] = {"elevinth ","twelveth ","thirteenth ","fourteenth ","fifteenth ","sixteenth ","seventeenth ","eighteenth ","nineteenth "};
const char place_tens [9] [20] = {"tenth ","twentieth ","thirtieth ","fourtieth ","fiftieth ","sixtieth ","eightieth ","ninetieth "};
const char place_larges [5] [20] = {"hundredth ","thousandth ","millionth ","billionth "};
//************************************************************************************************************************************
char* two_digits (const short&);
//returns a string saying what is the twodigited number it has as an argument.
char* strcat_ (char* ,const char*);
//this function is the same as he standard library function, you can remove it if you want and use the standard function instead
//i had to put it for my own purposes.
char* tri_digits (short* );
//this function takes two arguments, an array of size 2, the first place will hold the hundreds, the second will hold
//the tens and singles digits, it calls two_digits for the tens and singles
//it returns a pointer to a string containing the words of the numbers.
char* word_of (int );
//this is the main function which will be used solely in your program, this function recieves the rawinteger, and arranges
//the calls to other functions in such a way generating a string that describes the integer, finally it returns a pointer to that string,
//this function takes a copy of it's argument because it needs to change it ot performe it's role correctly.
short num_of_digits (int );
//this function returns the number of digits it's argument has, the return type is short to lighten pressure on the memory.
short* devide_into (short ,short* );
//this function recieves two arguments, the first is a tri_digited decimal integer, the second is a pointer to an array of size 2, it puts
//the hundreds of the integer in the first place of the array, and puts the rest of the integer in the second place, then returns a pointer
//to that array.
int my_pow (int ,int );
//this is also the same as the standard function pow(), and i also added it for my own purposes, you can actually remove it and include in this header
//the library <math.h> then use the standard pow() instead.
int& zero_last_left (int& );
//this function takes an int argument and zeroes it's highorder digit which is the last digit on the left.
int& zero_last_degree (int& );
//this function takes an integer argument, then it zeroes the highorder degree of it, that is: (134 234) becomes (000 234) or just (234)
// (23 514 925) > (514 925)
// ( 2 361) > (361).
// declarations.
//*************************************************************************************************************************************
// functions definitions :
char* two_digits (const short &x)
{
static char p[20];
for(int i=0;i<20;i++)
p[i]='\0'; //set all elements to '\0'
if ( (x<20) && (x>10) )
return (char*)teens[x11];// if(10<x<20) it is a teen number.
else if ( x<10 )
return (char*)singles[x]; // if(x<10) it is a single number.
else if ( x<100 ) // if(20 < x < 100) it is a multi number.
{
strcat_( p , tens [ (x / 10)  1] );
strcat_( p , singles [ (x % 10) ] );
}
return p; //after generating the right twodigits string return it to the calling function.
}
// debugged successfully.
//*****************************************************
char* tri_digits (short *x )
{
static char p[35]={'\0'};
for(short i=0 ; *(p+i) ; i++)
*(p+i)='\0'; //set all elements to '\0'
strcat_( p , singles [ x[0] ]);//concatenates the number of hundreds in the tri_digited number at the end of the generated string.
if(x[0]) //concatenates the word "hundred" at the end of the generated string.
strcat_( p ,larges [ 0 ]);
if( x[1]<10 && x[1] && x[0]) // if( 0 < x[1] < 10 ) concatenate "and" at the end of the generated string.
strcat(p,"and "); // note that x[1] containes the first two digits of the tridigited number.
strcat_( p , two_digits ( x[1] ));// calls two_digits() upon x[1].
return p; //returns the generated string.
}
// debugged successfully.
//*****************************************************
char* strcat_ (char *p , const char *z)
{
char *ptr;
ptr=p;
for( ; * ptr ; ptr++ ); //seek to the end of string p.
for(short i=0 ; *(z+i) ; i++ )
*(ptr+i)=*(z+i);
return p;
}
// debugged successfully.
//*****************************************************
char* word_of (int x)//the main function in the file.
{
static char p[1024]; //the string that will hold everything.
for(short n=0 ; n<1024 ; n++)
p[n]='\0';
short tri[2] , *short_int; //both will hold tridigited combinations of x, each in it's own way.
short degrees = num_of_digits(x)%3 ? (num_of_digits(x) / 3) +1 : num_of_digits(x)/3;
//degrees will hold how many degrees are there in the integer.
for( ; degrees ; degrees ) //this algorithm will apply itself to each degree individually.
{ // a degree is everthing between [10^3n , 10^3(n+1)], like the thousands or the millions.
short_int = devide_into( x/my_pow(10,(degrees1)*3) , tri );
/* x/10^3(degree1) is the last degree of x
if x= 235 215 352 then the last degree is 235.
if x= 42 123 then the last degree is 42.
if x= 1 957 315 250 then the last degree is 1.
devide_into() will put that last degree of x into the array tri, hundreds in the first place, the rest in the second place.
short_int will recieve the result of devide_into().*/
strcat_( p , tri_digits( short_int ) );
if(short_int[0]  short_int [1] )
if(degrees1)//if the degree is a thousand or bigger concatenates its name at the end of the generated string.
strcat_( p , larges [degrees1]);
zero_last_degree(x);//removes the last degree so that the algorithm can continue working decrementally on the integer.
}
return p;
}
// debugged successfully.
//*****************************************************
short num_of_digits (int x)//returns the number of digits in x.
{
register short i=0;
while(x)
{
i++;
x/=10;
}
return i;
}
// debugged successfully.
//*****************************************************
short* devide_into (short x, short *p)
{
p[0] = x/100;//put the hundreds of x into the first place of p.
short i=x,j;
if ( x<100 )//if (x<100) put all of it in the second place of p, then return p.
{
p[1]=x;
return p;
}
for(j=0 ; i>=10; i/=10 , j++);//these two lines plus the expression (xi) in the third line will
for( ; j ; i*=10 , j); //remove the third digit in x.
p[1] = (xi);//now x is less than 100, assign to second place of p, the first place is already assigned the
//third digit of x, now will return p.
return p;
}
// debugged successfully.
//*****************************************************
