return 0 on success, -1 on fail (eg you might not handle very large numbers, or negatives).
For digits 1 to 9 the answer is easy. For 20-99 the numbers follow a pattern. For 11-19 you have to code as a special case. Then you can go from one hundred to 999 by taking N/100 to get the hundreds, converting to a digit, then taking N % 100 to get the digits, and converting as before. Once you get up to a thousand, a new pattern appears. You can have one hundred thousand, one hundred and nine. Similarly with millions and billions.
It's rather tricky to write, but not undoable. The trick is to work from the big numbers to the little, using / to get them billions, millions, thousands, hundreds and tens, and using % to get the remainder.
couldn't grab the idea of 'converting number to words'.. but if you mean converting an int(or long int) to string, try using the itoa(), or ltoa() functions
Compilers are allowed to put extra functions into the standard headers, like stdio.h. It's a terrible rule, because it leads to confusion over what is and what is not standard. Particularly because the standard library lacks some pretty basic functions like min(), strdup(), readdir().
Also, the IO is deliberately rather limited to line-based input and output. So you have to include curses or conio.h to get colours or a clear screen. This isn't too bad, because a lot of the time in portable programming you can't depend on what sort of terminal interface you will have.
wow! i would like to know more about "standard" c/c++ functions.. where do i get them?? i'll try searching them out, but a more specific address would be great
hi, For converting numbers to word(to a string ) there is a command in string.h library named itoa.
prototype of function.
itoa(int value,char * string,int radix);
Here value is the number which you want to convert & string is your word string.Here radix is for base of number.You can take it from 2 to 36.But in normal use it is 10. If you have any other query you can ask on [email protected] BYE.
: For converting numbers to word(to a string ) there is a : command in string.h library named itoa.
Perhaps you should read the whole thread before posting...
itoa is (still) not a valid ISO C/C++ function. If you find it in string.h, it is only because you are using a poorly designed compiler that puts its own non-standard functions in standard headers.
There is no reason to use that function. The only excuse for using it is that you are a beginner who can't write a simple int to ASCII routine yet. There is no excuse for professional programmers to use it.
For those who are still beginners at C, here is the full code, for your convenience.
Comments
int numbertowords(char *words, int N)
return 0 on success, -1 on fail (eg you might not handle very large numbers, or negatives).
For digits 1 to 9 the answer is easy. For 20-99 the numbers follow a pattern. For 11-19 you have to code as a special case.
Then you can go from one hundred to 999 by taking N/100 to get the hundreds, converting to a digit, then taking N % 100 to get the digits, and converting as before.
Once you get up to a thousand, a new pattern appears. You can have one hundred thousand, one hundred and nine. Similarly with millions and billions.
It's rather tricky to write, but not undoable. The trick is to work from the big numbers to the little, using / to get them billions, millions, thousands, hundreds and tens, and using % to get the remainder.
but if you mean converting an int(or long int) to string, try using the itoa(), or ltoa() functions
(value % 10) + '0';
value /= 10;
Also, the IO is deliberately rather limited to line-based input and output. So you have to include curses or conio.h to get colours or a clear screen. This isn't too bad, because a lot of the time in portable programming you can't depend on what sort of terminal interface you will have.
Here's the Wiki.
[link=http://en.wikipedia.org/wiki/C_standard_library]http://en.wikipedia.org/wiki/C_standard_library[/link]
Beware, C99 is now the offical standard, but hardly any compilers implement it fully, though most implement bits of it.
For converting numbers to word(to a string ) there is a command in string.h library named itoa.
prototype of function.
itoa(int value,char * string,int radix);
Here value is the number which you want to convert & string is your word string.Here radix is for base of number.You can take it from 2 to 36.But in normal use it is 10.
If you have any other query you can ask on [email protected]
BYE.
: command in string.h library named itoa.
Perhaps you should read the whole thread before posting...
itoa is (still) not a valid ISO C/C++ function. If you find it in string.h, it is only because you are using a poorly designed compiler that puts its own non-standard functions in standard headers.
There is no reason to use that function. The only excuse for using it is that you are a beginner who can't write a simple int to ASCII routine yet. There is no excuse for professional programmers to use it.
For those who are still beginners at C, here is the full code, for your convenience.
[code]void getDecStr (unsigned char* str, unsigned char len, unsigned long val)
{
unsigned char i;
for(i=1; i<=len; i++)
{
str[len-i] = (unsigned char) ((val % 10UL) + '0');
val/=10;
}
str[i-1] = '';
}[/code]
Now promise to use this code and not some non-standard, non-portable crap! :-)