Need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Coeli
Forum Newbie
Posts: 2
Joined: Tue May 17, 2005 9:13 pm
Contact:

Need help

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I recently saw a function that did this exactly. Trying to track it down.
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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
Coeli
Forum Newbie
Posts: 2
Joined: Tue May 17, 2005 9:13 pm
Contact:

Post 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]
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

wtf? I gave you the program! O.o
Post Reply