String and I/O

This month's exercises concentrate on simple input/output and string functions. As in last month's exercises, the complexity increases a bit as you work down the list. (11), (12) and (13) may burn up some time, especially for novices. Try at least one exercise from this latter group.

  1. Using a text editor, generate a file that contains a few lines with one integer per line. Write a REXX program that will read the file and calculate the arithmetic average of these integers.

  2. Expand on Exercise 1 by inserting a few lines into your integer file that contain more than one integer per line.

  3. Write a variation of your average program that accepts input from the keyboard. One or more integers can be typed on one or more lines. You'll have to devise a scheme to indicate when you are finished typing. When you are done, display all the numbers and their average.

  4. Expand your keyboard average program to write the numbers to a file, then use your Exercise%nbsp;1 or Exercise 2 program to read the file and check your results.

  5. Write a routine that will place commas in a number according to United States conventions. (Never more three adjacent digits to the left of an actual or implied decimal point.) Be sure to test your routine with the following numbers: 1, 10, 100, 1000, -1000, +1000, 1000., 1000.1, 1234567890.0956, and consider how you'd like to handle +0123456789.

  6. Explore the system Time and Date functions by displaying all possible formats. Even if you are a fast typist, avoid the obvious solution of typing one function call for each possible format. Pass a string describing all the formats into a loop that uses a single Time and/or Date function call. Inside the loop, work through displaying the various formats as described by the string elements.

  7. Capture a web page, such as the home page of the OS/2 SIG, to your hard drive and write a REXX program to scan the file and make a list of all the links on that page.

    For those who have never composed web pages, they are simple text files and "links" have the form:

    Click here to visit our site.
    

    href=" ... " is the URL that will be used by the browser and "Click here" is the highlighted text that the viewer will see (and possibly click). "to visit our site." is a continuation of the regular text. The <a ... > and </a> "tags" must be paired and enclose the highlighted text.

    Be careful -- browsers don't care about mixing upper and lower case or excess white space. ("White space" is defined as one or more spaces or tab characters. There is an implied white space at the end of each line.) Expressing the above line of HTML as:

    <A
    href="http://www.phillyos2.org">Click
    here</a>
    to
    visit our site.
    

    is perfectly valid as far as a web browser is concerned and will render identically to our single line version above. Practical versions of this type of program will generally list the URL and the highlighted text side by side. In an actual application we are developing, links are captured from the Netscape Bookmark file and a web page, containing only links from certain sections, is built by the REXX program.

  8. We've all smiled when we saw one, and possibly have fabricated a document where one letter was replaced with another. Write a REXX program to replace a letter of choice with another.

  9. You are the cipher officer of the local counterespionage unit. Write a program to encode a document using a simple rotating letter replacement scheme. A rotation of two would replace "a" with "c", "b" with "d", "z" with "b", etc. On a moment's notice your spy master will give you the name of a file and the rotation for the message.

  10. Remember "Pig Latin"? Write a program to translate simple text from English to Pig. Our variant of Pig simply adds "ay" to the end of each word. If the word contains more than three characters, rotate the first letter to the end before adding the "ay".

  11. Save this web page to a file and generate a histogram of letter frequencies. Your output should have the form:

    <A> (235) ------------
    .
    .
    .
    <Z> (1)   -
    <b> (95)  -----
    .
    .
    .
    <z> (0)
    

    Where each dash represents a certain number of occurrences. Including a line such as "<z> (0)" for a nonexistent letter is a matter of personal style.

  12. Scan the words in an input file and gather statistics about word usage. We would like to know how many words beginning with the letter "a", "B", "c", etc., contain one, two, three, or whatever, number of characters. Your output should be a table with the number of characters per word as the column headings and each letter having its own row. Throw some generalized text at your program. Can it deal with punctuation? Hyphenated words? Contractions? How about a web page? Should text inside the HTML tags be considered?

  13. Previously, HTML tags were not case sensitive. "&LT;b&GT" was interchangeable with "<B>". In the future, lower case tags will be the rule and upper case tags may create problems for browsers. The page you are viewing is a mixture of old and new. Write a program to translate upper case tags to lower case. Don't go overboard on this exercise because it could become a mini career. This is a relatively simple page, concentrate on what you find here and don't worry about what you might find in the wild. You will have to decide how to handle text inside of double quotes, and you really don't want to translate anything contained within a <pre> </pre> pair of tags.

As always, there is no single "correct" way to approach these problems and a cute program that almost works is never as valuable as one that does work.

Bits and pieces of old code have a habit of being repurposed and used again and again. Keep these and last month's programs around because we'll revisit some of them next month.

Don't be embarrassed by your mistakes; bring them to class or post them on the bulletin board at http://os2source.vf.pond.com/ceilidh/. We can always learn from mistakes, success may be accidental.


Rexx Group Charter
Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4
Exercises 1 | Exercises 2 | Exercises 3
Answers to exercises
Exercises 1
Example code
Stock Market | Stock Market_a


About these pages
Content updated: 07-09-2005 10:07am

Page building by: PPWizard Content updated
07-09-2005 10:07am
Valid HTML 4.01!Valid CSS!