CM151: Lab 5 -- Fun with Strings!


Lab Goal:

Welcome to the fun, fun, fun world of strings, lists, and files. Hooray.



Colonel Meow is even more thrilled than you.
 

You may work in groups of two or individually.

Assignment

  1. A Caesar cipher is a simple substitution cipher based on the idea of shifting each letter of the plaintext message a fixed number (called the key) of positions in the alphabet. For example, if the key value is 2, the word "Sourpuss" would be encoded as "Uqwtrwuu." The original message can be recovered by "re-encoding" it using the negative of the key.

    Write a program that can encoe and decode Caesar ciphers. The input to the program will be a string of plaintext and the value of the key. The ouput will be an ecoded message where each character in the original message is replaced by shifting its key characters in the Unicode character set.

    For example, if ch is a character in the string and key is the amout to shift, then the character that replaces ch can be calculated as: chr(ord(ch) + key).

    Note that the encoded message may include characters that are not letters:
    >>> print(chr(ord("z") + 1))
    {




  2. Write a program that calculates the average word length in a sentence entered by a user.



  3. Word Count. A common utility on Unix/Linux systems is a small program called "wc". This program analyzes a file to determine the number of lines, words, and characters contained therein. Write your own version of wc. The program should accept a file name as input and then print three numbers showing the count of lines, words, and characters in the file.



  4. Optional (nothing to submit).
    Create a small program that doesn't exist on Unix/Linux, called wcat. This program should analyze a file and tell the user how many times "cat" appears in the text, both in total, and as a word. For instance, consider the following sentence:
    My cat played catch and then became catatonic.
    In this case, your program would tell you that there were three instances of the string cat, and one instances of the word cat.

Submission

Once you have finished your three programs, submit them to Moodle.