On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

## String Exercises

1. Check Amount Writing. Translate a number into the English phrase.

This example algorithm fragment is only to get you started. This shows how to pick off the digits from the right end of a number and assemble a resulting `string` from the left end of the `string`.

Note that the right-most two digits have special names, requiring some additional cases above and beyond the simplistic loop shown below. For example, 291 is "two hundred ninety one", where 29 is "twenty nine". The word for "2" changes, depending on the context.

As a practical matter, you should analyze the number by taking off three digits at a time, the expression ```(number % 1000)``` does this. You would then format the three digit number with words like "million", "thousand", etc.

Procedure 12.1. English Words For An Amount, n

1. Initialization

Set `result` ← ""

Set `tensCounter` ← 0

2. Loop. While n > 0

1. Get Right Digit. Set `digit` n % 10, the remainder when divided by 10.

2. Make Phrase. Translate `digit` to a `string` from "zero" to "nine". Translate `tensCounter` to a `string` from "" to "thousand".

3. Assemble Result. Prepend `digit` `string` and `tensCounter` `string` to the left end of the `result` `string`.

4. Next Digit.  n n ÷ 10. `tensCounter``tensCounter` + 1.

3. Result. Return `result` as the English translation of n .

2. Roman Numerals. This is similar to translating numbers to English. Instead we will translate them to Roman Numerals.

The Algorithm is similar to Check Amount Writing (above). You will pick off successive digits, using %10 and /10 to gather the digits from right to left.

The rules for Roman Numerals involve using four pairs of symbols for ones and five, tens and fifties, hundreds and five hundreds. An additional symbol for thousands covers all the relevant bases.

When a number is followed by the same or smaller number, it means addition. "II" is two 1's = 2. "VI" is 5 + 1 = 6.

When one number is followed by a larger number, it means subtraction. "IX" is 1 before 10 = 9. "IIX" isn't allowed, this would be "VIII".

For numbers from 1 to 9, the symbols are "I" and "V", and the coding works like this.

1. "I"
2. "II"
3. "III"
4. "IV"
5. "V"
6. "VI"
7. "VII"
8. "VIII"
9. "IX"

The same rules work for numbers from 10 to 90, using "X" and "L". For numbers from 100 to 900, using the sumbols "C" and "D". For numbers between 1000 and 4000, using "M".

Here are some examples. 1994 = MCMXCIV, 1956 = MCMLVI, 3888= MMMDCCCLXXXVIII

 Published under the terms of the Open Publication License Design by Interspire