Page 1 of 1

Need help

Posted: Sun May 29, 2005 8:51 pm
by Coeli
Can anyone give me the code for this:

DIGIT STRING
Suppose you translate a number into a string of digits spelled out, one word for each digit, followed by a single space. Writea program to accept a positive number and print out its DIGIT STRING. The program should work for decimal numbers as well.

Sample Run:
Enter a positive number: 407.8
The Digit String: FOUR ZERO SEVEN . EIGHT

Posted: Sun May 29, 2005 9:41 pm
by John Cartwright
I recently saw a function that did this exactly. Trying to track it down.

Posted: Sun May 29, 2005 9:44 pm
by Skara

Code: Select all

$string = &quote;134.98&quote;;
$bad = range(0,9);
$good = array(' ZERO',' ONE',' TWO',' THREE',' FOUR',' FIVE',' SIX',' SEVEN',' EIGHT',' NINE');
$string = str_replace($bad,$good,$string);
$string = substr($string,1); # takes out the first space
Edit: ^ posted while I was typing this. The function he wants is different from that other one.
Also, if you want a space before the period...

Code: Select all

$string = "134.98";
$bad = range(0,9);
$bad[] = '.';
$good = array(' ZERO',' ONE',' TWO',' THREE',' FOUR',' FIVE',' SIX',' SEVEN',' EIGHT',' NINE',' .');
$string = str_replace($bad,$good,$string);
$string = substr($string,1); # takes out the first space

Posted: Sun May 29, 2005 10:00 pm
by Coeli
This is what I did... But it doesn't work...and I only have about 30 minutes to finish the program..

Code: Select all

<form method="post">
<h3>Enter a positive number: <input type="text" name="vnum"></h3>
<input type="submit" value="Enter">
</form>

<?php
$vnum=$_POST['vnum'];

if ($vnum==NULL) {
    print "Enter a number";
	}
else if ($vnum=1) {
    print "ONE ";
	}
else if ($vnum=2) {
    print "TWO ";
	}
else if ($vnum=3) {
    print "THREE ";
	}
else if ($vnum=4) {
    print "FOUR ";
	}
else if ($vnum=5) {
    print "FIVE ";
	}
else if ($vnum=6) {
    print "SIX ";
	}
else if ($vnum=7) {
    print "SEVEN ";
	}
else if ($vnum=8) {
    print "EIGHT ";
	}
else if ($vnum=9) {
    print "NINE ";
	}
else if ($vnum=0) {
    print "ZERO ";
	}


?>
d11wtq | Please use

Code: Select all

tags when posting PHP code[/color]

Posted: Sun May 29, 2005 11:17 pm
by Skara
wtf? I gave you the program! O.o