Homework 4

1. Write a Ruby program that asks the user to enter a string of words separated by spaces. (NOTE: to simplify this problem, you can assume that your user will only enter strings consisting of letters and spaces. You don't have to worry about handling punctuation marks.) Your program should first convert the string to lowercase (see below to find links to Ruby's API documentation) and then use Ruby's regular expression capabilities (patterns, captures, match, sub, and/or gsub) to capitalize the first letter of each word in the string. Your program should then print the modified string to stdout. (2 points)

2. The home page of CSCI 3308 has a well defined structure. Each class announcement is contained in a <div class="entry"> element. Each such element has a well-defined structure in which there is one <p class="date"> element followed by one or more pairs of <p class="time"> and <p> elements. (Note: you do not have to handle entries that do not have this structure. For instance, the entries on September 8th and August 26th do not follow this pattern and do not need to be handled.) Write a Ruby program that uses Ruby's regular expression capabilities that exploits this structure to parse the file and print out each announcement to stdout. The output should look something like the text below. (8 points)


========================================
Thursday, September 21 2006

11:06:23 MDT: Lecture 8 is now available.

========================================
Wednesday, September 20 2006

13:26:19 MDT: The class schedule has been updated with information about the next three weeks of the semester.

========================================
Monday, September 18 2006

16:47:36 MDT: Lab 2 is now available. However, it is password protected. The TA will provide the username and password for the lab at your lab section.

========================================
Sunday, September 17 2006

15:11:16 MDT: Lecture 7 is now available. See you tomorrow in class!

========================================
Thursday, September 14 2006

9:09:23 MDT: Quiz 2 has been created and will be available starting at 11 AM tomorrow. Don't forget!

1:17:32 MDT: Homework 3 is now available.

========================================

…

Note: that some of the descriptions have embedded HTML tags in them. You should remove these tags before sending the description to stdout. For instance, one description on the home page looks like this:

Here is a pointer to a source of information about the APIs of various programming/scripting languages: <a href="http://www.gotapi.com">http://www.gotapi.com</a>.

Your program should strip away the HTML tags within the description; so for the above line, your program would print:

Here is a pointer to a source of information about the APIs of various programming/scripting languages: http://www.gotapi.com.

Submission

Please submit homework 4 by placing your two programs in a zip or tar archive and then uploading the archive to the moodle.

Background

To get input from stdin in a Ruby program, use a statement similar to the following:

input = gets.chomp!

This will read input from the user until they enter a newline and then “chomp” off the newline and store the resulting string in the variable input. (You can call the variable anything you want.)

Go to <http://www.ruby-doc.org/> to find Ruby-related documentation. For instance, you can find the documentation on Ruby's string class by going to <http://www.ruby-doc.org/core/> and then clicking on “String” in the list of Classes.

Finally, you can find documentation about Ruby's conditional and looping statements at <http://www.rubycentral.com/book/tut_expressions.html>.

If you have any questions about the Ruby programming language, don't hesitate to send e-mail to Professor Anderson and he will help you through any problems you may encounter.