I m trying to find idea to change like from 1 to one and 2 to two and 3 three from 1 to 999 i m had an idea to do like a string with information etc ? but i want better idea so i dunno how to do it :S lolz.

Think Check Writing Program

``````ary [] = { Zero, One, ...., Nineteen Twenty, Thirty, Fourty, ... Ninety, Hundred,  Thousand };

if (n <= 19)
ary[n]
else if (n < 100)      // 20...99
j = ary[  (n / 10) + 18 ]          // Get 10's
k = ary[ n % 10 ]                   // get 1's  if zero ignore
if ( k == 0) then ignore

etc.``````

What the poster above posted might look confusing, but what he is saying is a good way of doing this: Each String in the array will be the "name" of the index it is at. So index 0 of the array is named zero, and so on. This means in order to find the name of "20", you would use array[20] and it would return "twenty" since that is what you stored there.

The implementation is a little different - for example in C, I think an array of Strings is actually a 2D array of chars, but you get the point, I hope.

Ah, 20 was on the edge, but 21 would involve hitting the list twice.
Once for "twenty" and again for "one" then glue the two halves together for "twenty-one"
30 would be "thirty" since no ones remainder on the modulus then no second noun.
134 would be "One" + "Hundred" "Thirty" - "Four"

So twenty-seven ASCII nouns would cover one thousand numbers.

but i still i would have to do tests from 1 to nineteen
whish would be alot of tests i thought of this idea but is there another way i could do it by

No, For a number up to 999 you have 4 tests?

If number is 0 to 20 then use it as a string table index.
if greater then that then turn it into 10's and 1's
And use them as separate indexes!

``````char *szNoun[]= {
"zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
"eighty", "ninety", "hundred", "thousand" };

// n is number
if ( n <= 20)
print szNoun[ n ];
else if (n < 100)
j = n / 10;       // { 2...9 }
k = n % 10;    // { 0...9 }
print szNoun[ j + 18 ];
if ( k != 0 )
print "-"    szNoun[k];

// and the rest for you to figure out!``````

till now i made but i tried with 21 it didnt work out

``````#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
"sixten","sevten","eighteen","nineteen"
};
char *string2[]= {
"twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","hundreed"
};
//from int to array and return char string
char *IntToarray(int num)
{
char *pch;
int j=0;
if(num>0 || num<=19)
pch=string[num];
else if(num>=20 || num<=100) {
j=num %10;
#ifdef DEBUG
printf("number of j is %d",j);//it should out here 1 and it didnt
printf("number of j is %d",j);
if(j==1)
puts("dude so cool");
#endif
getchar();
num=num %10;
strcat(string2[num]," ");
strcat(string2[num],string[j]);
}
}
int main(void)
{
char *pch;
pch=IntToarray(21);
//puts(pch);
getchar();
return 0;
}``````

i dunno how its possible to do it with 1 string only like if i get 20 or thirty fourty fifty etc etc i think that should be in a array itself so i get the reminder by j then after that cancnate it to the string2
then as for 100 and up i have to do extra work but i will figure that out i just wanna fix from 20 to 99 first then do from 100 to 999

You really need the dwarf array for optimization.
you have a math bug.

``````char szBuf[ 80 ];
strcpy( szBuf, string1[ num ];

else if(num>=20 || num<100) {
j=num %10;
#ifdef DEBUG
printf("number of j is %d",j);//it should out here 1 and it didnt
printf("number of j is %d",j);
if(j==1)
puts("dude so cool");
#endif
getchar();
num=num / 10;
strcpy( szBuf, string2[ num ] );
if (j)
{
strcat( szBuf, "-" );
strcat( szBuf, string1[ j ] );
}
}

printf szBuf``````

I used a single string array for ease! Merely used a delta index for the extra data.

index 1's
index + 18 10's
index + 29 100's
index + 30 1000's

``````char *IntToarray(int num)
{
char *pch;
int j;
char temp[100];
if(num>0 || num<=19)
pch=string[num];
if(num>=20 || num<=100) {
if(num>=20 || num <=29) {
j=num %10;
strcpy(temp,string[20]);
strcat(temp," ");
strcat(temp,string[j]);
puts(temp);
}
}
}``````

i didnt read the other posts after i posted just now but i think my codes are just sloppy now i was thinking of like searching 20 30 40 50 60 but that will take alot of functions maybe i should write that into a function

You have to use a scratch buffer to build a concatenated string. In your implementation you were contaminating the original source nouns by concatenating to them!

Using your latest post, it still has the math error.

``````char *IntToarray(int num)
{
char *pch;
int j;
char temp[100];
if(num>0 || num<=19)
pch=string1[num];
else  if(num>=20 || num<=100) {

j=num %10;
num = num / 10;

strcpy(temp,string2[num]);
if (j != 0)
{
strcat(temp,"-");
strcat(temp,string1[j]);
}

puts(temp);
}
}``````

Using the single array

``````char *IntToarray(int num)
{
char *pch;
int j;
char temp[100];
if(num>0 || num<=19)
pch=string[num];
else  if(num>=20 || num<=100) {

j=num %10;
num = num / 10;

strcpy(temp,string[num + 18]);
if (j != 0)
{
strcat(temp,"-");
strcat(temp,string[j]);
}

puts(temp);
}
}``````

I've got to run but does this make sense now?
I'd recommend using one single buffer, and printing it when the entire string is built!

Also note, the cases of { 20, 100 } can be handled wither way as the result is the same index!

So with a little cleanup...

``````char *IntToarray( char *szBuf, int num)
{
int j;

if(num>0 || num<=19)
strcpy( szBuf, string[num] );
else  if(num>=20 || num<=100) {

j=num %10;
num = num / 10;

strcpy( szBuf, string[num + 18]);
if (j != 0)
{
strcat( szBuf, "-");
strcat( szBuf, string[j]);
}
}

return szBuf;    // return the passed in buffer!
}``````

Missed a typo in your original. Zero was being ignored!
And only need to check for the upper range after the first test!

``````char *IntToarray( char *szBuf, int num)
{
int j;

*szBuf = 0;   // Preset for error case!

if (num>=0 && num<=19)
strcpy( szBuf, string[num] );
else  if( num<=100) {
j=num %10;
num = num / 10;

strcpy( szBuf, string[num + 18]);

if (j != 0)
{
strcat( szBuf, "-");
strcat( szBuf, string[j]);
}
}

return szBuf;    // return the passed in buffer!
}``````

okay i almost done it for but when i return it to char * it messed it up

``````#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
"sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","hundreed"
};
//from int to array and return char string

/* n will be the  index of the array like if its twenty thirty fourty etc*/
/*n2 will be the reminder*/
void ParseString(char *buffer1,char **buffer2,int n,int n2)
{
strcpy(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
char *IntToarray(int num)
{
char *pch;
int j;
int z;
char temp[100];
if(num>0 || num<=19)
pch=string[num];
if(num==20 || num <100) {
j=num % 10;
num=( num /10) +18;
if(j==0)
return pch=string[num];
ParseString(temp,string,num,j);
//puts(temp); this work i dunno why it get messed
return pch=temp;//this doesnt work
}
}
int main(void)
{
char *pch;
pch=IntToarray(21);
puts(pch);
getchar();
return 0;
}``````

i did till now but i got problem now now it when i get to like 111 or more i will get reminder 1 etc so it wont be from one hundered eleven it will be one hundered 1

``````#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
"sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","one hundreed",
"two hundered","three hundered"
};
//from int to array and return char string

/* n will be the  index of the array like if its twenty thirty fourty etc*/
/*n2 will be the reminder*/
#define LESSTHAN100 0
#define MORETHAN100 1
void ParseString(char *buffer1,char **buffer2,int n,int n2,int choice)
{
if(!choice) {
strcpy(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
else {
strcat(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,"and");
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
}
char *IntToarray(int num)
{
char *pch;
int j;
int z;
char temp[100];
if(num==0 || num<=19){
puts("we returned");
return pch=string[num];
}
else if(num==20 || num <100) {
j=num % 10;
num=( num /10) +18;
if(j==0)
return pch=string[num];
ParseString(temp,string,num,j,LESSTHAN100);
puts(temp);
}
else if(num==100 || num<=999) {
j=num % 10;
num= (num/10) +18;
if(j==0)
return pch=string[num];
ParseString(temp,string,num,j,MORETHAN100);
puts(temp);
}
}
int main(void)
{
char *pch;
pch=IntToarray(41);
//puts(pch);
getchar();
return 0;
}``````

anyways i will go sleep now i hope tommrow i will fix those errors.

You're commiting a cardinal sin. Returning a local stack buffer from a function! Don't do that unless you're sure you a single-threaded and that buffer is declared static so it exists always.

NEGATIVE numbers aren't handled!
if == 0 or <= 19 NO That means negative numbers are crunched like positive numbers!
It has to be a range 0 <= N && N <= 19
You're still using the conditional OR || instead of AND.

And you don't need a range check on the next numbers. ONLY < 100

Please review that function I gave you. Single step it and examine each step to see how it works.

Also When you're ready, flip the function backwards.

if value == 0 handle special!

if non zero value < 1000

reduce number

if non zero value < 100

reduce number

handle < 20

yah i also made a mistake of || instead of && i wanted now i m stuck
after 111 it will as i said before get the reminder whish is like 111 reminder is 1 so it will print for example 111 one hundered and one I dunno how can i fix this...

even it start before 111 it start at 110 when i % it it will return the same num so there no search list with that num so it mess up the print

but why when i made it static it worked i didnt return the acctual temp i return the char * ptr i returned ptr = ?

if n == 0 "Zero" return;

Hundreds
h = n / 100 h:{0...9}
if (h) printf string[ h ] + string[28]
n -= h * 100

Tens left

if (n <= 19) print string[n]
else {
t = n / 10
o = n % 10
print string[t+18]
if (o) print "-" + string[o]
}

Does that make sense?
Post your entire code string[] and parser!

no i acctually dont quite understand that i understand the daviding part but whats if h? and why would you - it with the h after that? anyways my code till now

``````#include <stdio.h>
#include <string.h>
#define DEBUG
char *string[]= {
"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
"sixten","sevten","eighteen","nineteen","twenty","thirty","fourty","fifty","sexty","seventy","eighty","nighty","one hundreed",
"two hundered","three hundered"
};
//from int to array and return char string

/* n will be the  index of the array like if its twenty thirty fourty etc*/
/*n2 will be the reminder*/
#define LESSTHAN100 0
#define MORETHAN100 1
void ParseString(char *buffer1,char **buffer2,int n,int n2,int choice)
{
if(!choice) {
strcpy(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
else {
strcat(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,"and");
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
}
char *IntToarray(int num)
{
char *pch;
int j;
int z;
static char temp[100];
if(num>0 && num<=19){
puts("we returned");
return pch=string[num];
}
else if(num>19 && num <100) {

j=num % 10;
num=( num /10) +18;
if(j==0)
return pch=string[num];
ParseString(temp,string,num,j,LESSTHAN100);
return pch=temp;
}
else if(num>100 && num<=109) {
j=num % 10;
num= (num/10) +18;
if(j==0)
return pch=string[num];
ParseString(temp,string,num,j,MORETHAN100);
return pch=temp;
}
}
int main(void)
{
char *pch;
pch=IntToarray(109);
puts(pch);
getchar();
return 0;
}``````

also making num % 20 will solve the problem but that will stay only till 119 !! after that it wont work

i can make couple if if statement to fix that but that will take alot of tests since i want a converter till 999

Here, this is untested but I've reworked your function.

You should examine how the hundred's are handled to add thousand handling!

``````char *string[]= {
"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","tweleve","thirteen","fourteen","fifteen",
"sixten","seventeen","eighteen","nineteen",         						// 0...19
"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety",	// 20...27
"hundred", "thousand", "million"											// 28...30
};

void ParseString(char *buffer1,char **buffer2,int n,int n2,int choice)
{
if(!choice) {
strcpy(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
else {
strcat(buffer1,buffer2[n]);
strcat(buffer1," ");
strcat(buffer1,"and");
strcat(buffer1," ");
strcat(buffer1,buffer2[n2]);
}
}

bool IntToarray( char *szBuf, int num)
{
int h, t, o;
bool bRet;
char *pStr;

if (num == 0)			// Special Case!
{
strcpy( szBuf, string[0] );
return true;
}

*szBuf = 0;
bRet = false;
pStr = szBuf;

//	negative number?
if (num < 0)
{
pStr += sprintf( szBuf, "negative " );
num = -num;
}

// thousands or higher not handled!

if ( num >= 1000 )
{
*szBuf = 0;
return false;
}

//	Hundreds

if ( num >= 100 )
{
h = num / 100;		// hundreds digit {0...9}

if (h)
{
num -= (h * 100);	// remainder
pStr += sprintf( pStr, "%s %s ", string[ h ], string[28]  );
}
}

if (num >= 20)
{
t = num / 10;
num = num % 10;

pStr += sprintf( pStr, "%s-", string[ t + 18 ] );	// 10's digit
}

if (num )   // 0....19
{
strcat( pStr, string[ num ] );	// 1's to teen's
}

return bRet;
}

int main(void)
{
char szBuf[ 100 ];

pch=IntToarray( szBuf, 109);
puts( szBuf );

getchar();
return 0;
}``````