By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

Convert numbers to words

P: n/a
How can we write a shortest program in c to convert given number to
words format. example input 5012 Output: five thousand tewlve.
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 4 Jan 2005 20:29:01 -0800, ab***********@gmail.com (Abhishek Jha)
wrote in comp.lang.c:
How can we write a shortest program in c to convert given number to
words format. example input 5012 Output: five thousand tewlve.


You can use any general purpose editor that can save files as plain
text.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

P: n/a
Abhishek Jha wrote:
How can we write a shortest program in c to convert given number to
words format. example input 5012 Output: five thousand tewlve.


If I interpret you literally, the shortest possible C program on a
platform with 32-bit integers would probably use the number to seek into
a 100GB file and read the string out. I imagine this is rather
unfeasible for a homework assignment.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #3

P: n/a
ab***********@gmail.com (Abhishek Jha) writes:
How can we write a shortest program in c to convert given number to
words format. example input 5012 Output: five thousand tewlve.


I've posted this here several times before. I have a version for
Japanese too if you want that.

#include <stdio.h>
#include <stdlib.h>

char *ordinal (char buf[1024], int value);

int
main (int argc, char *argv[])
{
char buf[1024];

if (argc != 2)
{
printf ("usage: ordinal <number>\n");
return EXIT_FAILURE;
}

ordinal (buf, atoi (argv[1]));
puts (buf);
return EXIT_SUCCESS;
}

/* Works for positive and negative numbers up to 9 digits long,
if you speak American English. */
char *
ordinal (char buf[1024], int value)
{
static const char *const powers[]
= {"thousand", "million", "billion"};

static const char *const ones[]
= {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};

static const char *const tens[]
= {"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};

char *cp = buf;

if (value < 0)
{
cp += sprintf (cp, "negative ");
value = -value;
}
else if (value == 0)
{
strcpy (buf, "zero");
return buf;
}

{
int part_stack[4];
int *part_ptr = part_stack;

for (; value; value /= 1000)
*part_ptr++ = value % 1000;

while (part_ptr > part_stack)
{
int p = *--part_ptr;

if (p >= 100)
{
cp += sprintf (cp, "%s hundred ", ones[p / 100 - 1]);
p %= 100;
}

if (p >= 20)
{
if (p % 10)
cp += sprintf (cp, "%s-%s ", tens[p / 10 - 2], ones[p % 10 - 1]);
else
cp += sprintf (cp, "%s ", tens[p / 10 - 2]);
}
else if (p > 0)
cp += sprintf (cp, "%s ", ones[p - 1]);

if (p && part_ptr > part_stack)
cp += sprintf (cp, "%s ", powers[part_ptr - part_stack - 1]);
}
}

cp[-1] = 0;
return buf;
}

/*
Local variables:
compile-command: "gcc -W -Wall -ansi -pedantic ordinal.c -o ordinal"
End:
*/

--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Nov 14 '05 #4

P: n/a
Ben Pfaff wrote:

ordinal (buf, atoi (argv[1]));
...
/* Works for positive and negative numbers up to 9 digits long,
if you speak American English. */
char *
ordinal (char buf[1024], int value)


You assume an int can store a 9 digit integer. I think you should
change the parameter to long, and (also) use strtol instead of atoi.
--
Peter

Nov 14 '05 #5

P: n/a
"Peter Nilsson" <ai***@acay.com.au> writes:
Ben Pfaff wrote:

ordinal (buf, atoi (argv[1]));
...
/* Works for positive and negative numbers up to 9 digits long,
if you speak American English. */
char *
ordinal (char buf[1024], int value)


You assume an int can store a 9 digit integer. I think you should
change the parameter to long, and (also) use strtol instead of atoi.


If you supply a 9 digit integer to it, assuming that it is
possible, it will convert it. That's all I was intending to
state.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #6

P: n/a
> I've posted this here several times before. I have a version for
Japanese too if you want that.

:)..i like that...will that Japanese version compile on ANSI C
compiler...-:)

Cheers
Waqas
------------------------------------------
"You laught at me koz im different, i laugh at you koz you all are
same" Kurt Cobaine

Nov 14 '05 #7

P: n/a
my*******@gmail.com writes:
I've posted this here several times before. I have a version for
Japanese too if you want that.

:)..i like that...will that Japanese version compile on ANSI C
compiler...-:)


It writes its output as romanized text, so it should compile and
run everywhere (modulo any bugs).

Here it is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *ordinal (char buf[1024], unsigned value);

int
main (int argc, char *argv[])
{
char buf[1024];

if (argc != 2)
{
printf ("usage: ordinal <number>\n");
return EXIT_FAILURE;
}

ordinal (buf, atoi (argv[1]));
puts (buf);
return EXIT_SUCCESS;
}

char *
ordinal (char buf[1024], unsigned value)
{
static const char *const powers[]
= {"man", "oku"};

static const char *const values[][9] =
{
{"sen", 0, "sanzen", 0, 0, 0, 0, "hassen", 0},
{"hyaku", 0, "sanbyaku", 0, 0, "roppyaku", 0, "happyaku", 0},
{"juu", 0, 0, 0, 0, 0, 0, 0, 0},
{"ichi", "ni", "san", "yon", "go", "roku", "nana", "hachi", "kyuu"},
};

static const char *const *const ones = values[3];

char *cp = buf;

if (value == 0)
{
strcpy (buf, "rei");
return buf;
}

{
int part_stack[4];
int *part_ptr = part_stack;

for (; value; value /= 10000)
*part_ptr++ = value % 10000;

while (part_ptr > part_stack)
{
int index[4];
int p, i;

p = *--part_ptr;
for (i = 3; i >= 0; i--)
{
index[i] = p % 10;
p /= 10;
}

for (i = 0; i < 4; i++)
{
int c = index[i];
if (c != 0)
{
if (values[i][c - 1] == 0)
cp += sprintf (cp, "%s%s ", ones[c - 1], values[i][0]);
else
cp += sprintf (cp, "%s ", values[i][c - 1]);
}
}

if (*part_ptr && part_ptr > part_stack)
cp += sprintf (cp, "%s ", powers[part_ptr - part_stack - 1]);
}
}

cp[-1] = 0;
return buf;
}

/*
Local variables:
compile-command: "gcc -W -Wall -ansi -pedantic ordinal-jp.c -o ordinal-jp"
End:
*/

--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.