# Robson » Little Red Book » Telegram Costing

## Chapter 8 - Question 4

Given a file of telegrams consisting of a sequence of 128-character blocks, except for the last block which may contain fewer than 128 characters, computer the cost of each telegram.

Telegrams are charged by the word at the rate of 17p per word, except that 'long' words of over 12 characters are charged at 25p each.

There is a minimum charge of one pound per telegram.

The telegrams file consists of upper-case letters and spaces only, except that telegrams are seperated from each other by a special seperator character - a '*', say.

It is advisable to assume that the telegrams file has already been validated and conforms to this structure.

### Solution 1

`<?`
`    // get all the telegrams in one long string`
`    \$telegrams = implode('', file(\$_SERVER['DOCUMENT_ROOT'] . '/littleredbook/telegrams.txt'));`
`    // seperate each telegram`
`    \$telegrams = explode('*', \$telegrams);`
`    // show each telegram`
`    foreach(\$telegrams as \$telegram)`
`    {`
`        // show the telegram`
`        echo \$telegram;`
`        // get each word into an array`
`        \$words = explode(' ', \$telegram);`
`        // loop through each word`
`        foreach(\$words as \$word)`
`        {`
`            // long words over 12 letters cost more`
`            if (strlen(\$word) > 12)`
`                \$price += 0.25;`
`            else`
`                \$price += 0.17;`
`        }`
`        // there's a minimum charge of one pound`
`        // so check if the price is less than that`
`        if (\$price < 1)`
`            // set the price to the minimum`
`            \$price = 1;`
`        // show the final price in normal format`
`        echo . number_format(\$price, 2) . '</b><br/> <br/>';`
`        // reset the price for the next telegram`
`        \$price = 0;`
`    }`
`?>`

Which produces for this telegrams file:

LOREM IPSUM DOLOR SIT AMET CONSECTETUER ADIPISCING ELIT UT TINCIDUNT IPSUM UT NISL CURABITUR SUSCIPIT NIBH VEL LOREM AENEAN ID TORTOR EGET EROS PLACERAT ADIPISCING = £4.25

MAECENAS DUI QUAM ALIQUET GRAVIDA HENDRERIT A ALIQUAM QUIS LOREM CRAS ELEIFEND QUISQUE TE MPUS VULPUTATE NULLA MORBI PORTA ODIO IN LOREM MAECENAS QUIS DOLOR PRAESENT COMMODO EROS VEL VELIT = £4.93

CURABITUR QUIS MASSA LOREM IPSUM DOLOR SIT AMET CONSECTETUER ADIPISCING ELIT CRAS LAOREET ANTE SIT AMET TORTOR UT CONSECTETUER CURSUS METUS ALIQUAM MAURI S LECTUS TEMPOR IN MOLLIS AT ORNARE VITAE IPSUM = £5.27

DONEC QUIS TURPIS VITAE DOLOR = £1.00

PHARETRA TINCIDUNT PROIN DIGNISSIM RISUS ET MAGN A DONEC PEDE SUSPENDISSE CONVALLIS CURABITUR NEC TELLUS FUSCE TELLUS LOREM RHONCUS ID SODALES EU MATTIS VITAE RISUS = £4.08

VESTIBULUM SIT AMET EROS NON DIAM ELEIFEND IACULIS MAURIS BLANDIT LUCTUS TORTOR SED IMPERDIET SEMPER NIBH ETIAM SCELERISQUE TINCIDUNT LAC US CURABITUR TORTOR RISUS CONSEQUAT MOLESTIE POSUERE VEL FEUGIAT NEC TORTOR NULLAM MAGNA = £5.35

ODIO PORTA EGET LAOREET BIBENDUM ADIP ISCING UT PURUS SED EGET NEQUE CRAS VITAE QUAM EU LIGULA CONDIMENTUM TEMPUS SED BLANDIT NUNC VITAE ERAT = £3.91

UT ET NISI SED QUAM VA RIUS VARIUS AENEAN VEHICULA NULLA SED VELIT SAGITTIS ALIQUET FUSCE ADIPISCING NEQUE NON RHONCUS FAUCIBUS VELIT ODIO HENDRERIT ODIO EU TINCIDUNT TORTOR DUI RUTRUM FELIS = £5.10

PELLENTESQUE IACULIS VENENATIS LOREM PHASELLUS PLACERAT LEO EU VELIT AENEAN ALIQUET CONSEQUAT TURPIS PRAESENT FERMENTUM SUSCIPIT ENIM MORBI LUCTUS = £3.23

LOBORTIS ELIT SED PORTA ALIQUET ENIM MORBI PRETIUM LOREM CONVAL LIS QUAM NAM QUAM DOLOR PORTA NEC FEUGIAT MALESUADA VOLUTPAT SED MI MAURIS PULVINAR PEDE NEC CURSUS LAOREET SAPIEN EROS FRINGI LLA FELIS ID SAGITTIS MAURIS TURPIS EGET LOREM = £6.29

INTEGER NISI RISUS TINCIDUNT SIT = £1.00

AMET TINCIDUNT SIT AMET PHARETRA ID FELIS VEST IBULUM METUS NEQUE ELEIFEND IN SOLLICITUDIN SED TINCIDUNT VITAE MAGNA PROIN QUIS SAPIEN AENEAN ET SEM ET ERAT ULTRICIES ORNARE NIBH QUAM ORNARE PORTA = £5.10

## Log

• November 6, 2005 - Added solution 1.
© 2004-19 robson | cc unless stated