Assignment 2

This assignment will give you some practice into writing simple programs that use strings and loops.

In this assignment, you will write two programs. Your programs should be runnable at the DOS command prompt by calling python program name.

Program 1. Encode and Decode

Secret codes have been used throughout history to protect sensitive information, and one of the most famous codes is called the Caesar cipher (because it's rumored that Julius Caesar used it). The Caesar cipher replaces each character by shifting it key characters forward in the alphabet. For example, if key equals 2, then the character "A" would be replaced by the character "C", and the string "Apple" would be replaced by the string "Crrng".

Your program should ask the user for a string, and the value of the key, and whether the user wants to encode or decode the string. It should then output the encoded/decoded string, depending on the user's preference.

A sample run of your program should look like the following:

J:\> python caesar.py
This program encodes/decodes a string using the Caesar cipher.
What is your string? Apple
What is the key you wish to use? 2
Do you wish to encode or decode (E/D)? E
The encoded string is Crrng
J:\>

Your program should preserve capital and small letters, and deal with the last letters of the alphabet by "looping" around to the beginning. In other words, if key equals 2, "Y" should be encoded by "A" and "Z" should be encoded by "B".

Your program should be called caesar.py.

Program 2. Word Count

A common utility on Unix/Linux systems (that you will be using next semester) is a small program called wc, short for "word count". This program reads in a text file, and counts the number of lines, words and characters in it. xs. It then outputs the result.

Write a program that will do the same thing. A sample run of your program should look like the following:

J:\> python wordcount.py 
This program counts the number of characters, words and lines in a file.
What is the name of the file? asgn2.html
Your file contains 57 lines, 446 words and 2816 characters.

Your program should be called wordcount.py.

Due Date

Your homework is due before 7am, Monday October 19, on WebCT (Hand in two separate programs). No exceptions. We will not accept late handins any more.

Anti-Cheating Policy

This is to remind you that COMP 201 has a strict no-cheating policy. The penalties for cheating are on the course webpage. Please reread those policies -- don't say that we didn't warn you!