CSCI 3310
Fall 2007
Small Programming Assignment 1

Assigned: August 23
Due: August 31, 11:59pm


HTML and CGI

There are a variety of ways to make web pages that respond to requests. One is called the Common Gateway Interface, or CGI. The user fills in a form on a web page, and submits the form to a server. The server runs a program that receives information on the form and produces a new page for the browser to display.

This page does not explain how CGI works, except one aspect of that. Between the user input and the program that responds to the request, strings are converted into a form that avoids certain special characters. The following conversions are done.

  1. Each space is replaced by +.
  2. A special character (including, for example, +) is replaced by a three-character sequence %XX, where XX is the hexadecimal code for the character. For example, ':' is replaced by %3A.

The program that processes the request needs to undo those conversions. This assignment is to write a function that a CGI application can use for that purpose.


The assignment

Write a Java class Unmangler containing a static Java function called unmangle that takes a string as a parameter, and returns a new string that holds the string that you get by undoing the above conversions, with a null terminator. For example, unmangle("a+b%3A") should return "a b:".


Getting started

You will want to be able to convert a string of two hexadecimal digits to an integer (a character code). Here is a function that does such a conversion. You can use this as a helper for unmangle.

  /************************************************
   *              hexToInt                        *
   ************************************************
   * digs is a string of two hexadecimal digits.  *
   * Return the number that they represent.  For  *
   * example, "2A" represents 2*16 + 10 = 42.     *
   ************************************************/

    public static int hexDigitToInt(char dig)
    {
	if(dig >= '0' && dig <= '9') return (int) dig - '0';
        if(dig >= 'A' && dig <= 'Z') return (int) dig - 'A' + 10;
        if(dig >= 'a' && dig <= 'z') return (int) dig - 'a' + 10;
        return 0;
    }

    public static int hexToInt(String digs)
    {
	return 16*hexDigitToInt(digs.charAt(0)) 
               + hexDigitToInt(digs.charAt(1));
    }

Submitting your work

Put your class in a file called Unmangle.java. Make the unmangle function public and static. Be sure to include the hexToInt function as well, if you use it. If there is a main program, for testing, then make a copy of your program, remove the main program from the copy, and submit that.

Put your file somewhere in your home directory in the lab. Change to the directory that contains your file. For example, if it is in directory 3310, then do command

  cd 3310
Then submit using command
  ~karl/3310/bin/submit S1 unmangle.java
You should get a confirmation that the file was successfully submitted.