package form.run;

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

public class UrlEncoder {
  public static String encode(String orig) {
    StringBuffer encoded = new StringBuffer();
    int i=0,curr;
    String charCode;
    char currentChar, decodedChar;
    while(i < orig.length()) {
      currentChar = orig.charAt(i);
      curr = (int)currentChar;
      if (currentChar == ' ') {
        encoded.append("+");
        i = i + 1;
      } else if ( ((currentChar < 45) || ( (currentChar > 57) && (currentChar < 65)) || ( (currentChar > 90) && (currentChar < 97) ) || (currentChar > 122) || (currentChar ==47)) && (currentChar!=95)) {
	if (curr >= 16)
	encoded.append("%"+Integer.toHexString(curr));
	else
	encoded.append("%0"+Integer.toHexString(curr));
        i = i + 1;
      } else {
        encoded.append(currentChar);
        i = i + 1;
      }
    }
    return(encoded.toString());
  }

  public static void main(String[] args) {
    System.out.println(encode(args[0]));
  }
  
}
