Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
converting numbers to words using string Posted by helen_18 on 16 Feb 2009 at 5:10 AM
please help me how to convert numbers to words using string
. . . .as soon as possible
Report
Re: converting numbers to words using string Posted by Malcolm_McLean on 16 Feb 2009 at 5:26 AM
You write a function

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.

Report
Re: converting numbers to words using string Posted by virtual_arts on 16 Feb 2009 at 5:37 AM
hey! thanks malcolm! your reply enlightened me....i couldn't get the idea of the problem at first.....
Report
Re: converting numbers to words using string Posted by virtual_arts on 16 Feb 2009 at 5:31 AM
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
Report
Re: converting numbers to words using string Posted by Lundin on 16 Feb 2009 at 6:00 AM
You should not use itoa() or ltoa() since those aren't valid C/C++ functions. They are quite easy to implement yourself, however. Here is a hint:

(value % 10) + '0';
value /= 10;
Report
Re: converting numbers to words using string Posted by virtual_arts on 17 Feb 2009 at 5:18 AM
iam sorry, i didn't get the meaning of 'valid' functions....i would appreciate it if you could clarify.....thanks in advance,,
:)
Report
Re: converting numbers to words using string Posted by Lundin on 17 Feb 2009 at 5:22 AM
They aren't part of the C nor C++ language: they are not standard C/C++.
Report
Re: converting numbers to words using string Posted by virtual_arts on 17 Feb 2009 at 5:44 AM
alright...thanks!! i just mentioned since i saw them in turbo c++ documentation
Report
Re: converting numbers to words using string Posted by Malcolm_McLean on 18 Feb 2009 at 10:07 AM
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.

Report
Re: converting numbers to words using string Posted by virtual_arts on 19 Feb 2009 at 5:25 AM
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 :)
Report
Re: converting numbers to words using string Posted by Malcolm_McLean on 19 Feb 2009 at 8:41 AM

Here's the Wiki.

http://en.wikipedia.org/wiki/C_standard_library

Beware, C99 is now the offical standard, but hardly any compilers implement it fully, though most implement bits of it.

Report
Re: converting numbers to words using string Posted by virtual_arts on 22 Feb 2009 at 11:05 PM
thank you bro1 i'll keep that in mind!
Report
Re: converting numbers to words using string Posted by napoxander on 21 Feb 2009 at 12:13 PM
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 napoxander@yahoo.com.
BYE.
Report
Re: converting numbers to words using string Posted by Lundin on 23 Feb 2009 at 2:22 AM
: 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.

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] = '\0';
}


Now promise to use this code and not some non-standard, non-portable crap!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.