Monday, August 30, 2010

HW 1: Hide The Message

File:ROT13 table with example.svg

The ROT 13 cypher is a simple method to hide a message so the casual observer cannot reader. It works by replacing each letter with one that is 13 spots later in the alphabet, so that a becomes n, b becomes o, and z becomes m, because we wrap around.

For this first homework you will implement a program that first asks the user for a number, then for a string, and then prints out the given string but rotated by the number of spots the user requested. You can assume that the string the user types is composed only of lower case letters [a-z]. Here are a couple of sample interactions with the program:

What offset do you want?1

Enter word to translate:abczyx
bcdazy
-----------
What offset do you want?13

Enter word to translate:hello
uryyb

You will implement this homework using typecasting and without using any if-then statements, we have not covered those anyway. You will need to use a for-loop.

It might at first seem that you need to know the values in the basic-latin unicode block, but you don't. All you need to know is that all the lower case letters appear consecutively and in alphabetical order in the table. Also note that if you need to know the unicode number of any character, say 'a', you can simply write a simple program with theh line System.out.println((int)'a'); If you are interested, the wikipedia unicode page tells more than anyone wants to know about Unicode.

You need to either upload your homework to our dropbox or email it to your TA by Tuesday, September 7 @9am.

Update Sep. 2: I messed up, again. I thought that Chapter 3 covered for-loops, but they are in Chapter 4 instead. So, you can read ahead on chapter 4 or just use the code below in your homework:

  String test = "Hi there!";
  //This for loop will print out the numbers from 0 to the length of the
  // string in the 'test' variable.
  for (int i=0; i < test.length(); i++) {
   System.out.println(i);
  }

That code will loop from 0 to the last position in the string test. You will need that to iterate over the letters in the word that the user inputs.

No comments: