CS 120 Project 4 – Strings and Characters

CS 120 Project 4 – Strings and Characters
Fall 2014 – due Fri Nov 7 (revised)

Write Python programs to perform the following calculations involving characters and strings. Each program should be divided into a main() procedure that performs input and output, and separate functions to do the calculations. Input is from the keyboard, and output is to the screen.

1.    Basic string operations:  The main program should define the following strings (no data entry is required):
A = “Moravian College”
B = “Greyhounds”
C = your first name in mixed case
D = “Python programming is easy!”
E = “2.71828”
Perform the following string operations in the main program. Your program code should show how the resulting string was obtained. Separate functions are not needed.
(a)    Concatenate strings A and B, with a space separating the substrings.
(b)    Multiply string B times 4.
(c)    Extract the substring “hounds” from string B and convert it to uppercase.
(d)    Convert string C (your first name) to lowercase, and reverse the characters in the string?
(e)    What is the length of string D if the spaces are removed?
(f)    Pick a vowel, and count the number of times that vowel appears in the lowercase         version of string C.
(g)    Convert string E to a floating point number.
(h)    Split the words in string D and place them in a list.
Output the results for parts (a) to (h).

2.    Palindromes:  The main program should input from the keyboard a single “word” consisting of letters, digits, and other symbols, and store it in a string variable W. The program should then call the function reverseWord(W) to convert W to lower case, reverse the order of the characters in the string, and then return the resulting string. For example, if the string “Forest!” is input, the function should return “!tserof”.
The main program should then output the reversed word. If the output word is identical to the input string (ignoring case), the string is symmetric, so your program should output “Palindrome”. Compile and run your program with the following input words.
(a)    Free-Beer
(b)    NoLemonNoMelon
(c)    #123XYx321#
(d)    LeBron:Norbel
(e)    aibohphobia

3.    Python Keywords:  The main program should input from the keyboard a single “word” consisting of letters and digits, and store it in a string variable W. The program should then call the function isKeyword(W) to check if W is a Python reserved word. Python words are case-sensitive, so don’t change the case of the input word.
If W is a Python keyword, then the function should return the logical value True. Otherwise, the return value should be the logical value False.
The main program should display the string message “Keyword!” when the function return value is True. Otherwise, the program should display “Not a Keyword”.
Compile and run your program with the following input words.
(a)    list
(b)    while
(c)    True
(d)    Class
(e)    print
(f)    return

4    Vowel Counts:  The main program should input a short string from the keyboard and store it in a variable S. The program should then call the function vowelCount(S) to count and return the number of vowels in the string. The program should then output the vowel count, along with the percent of characters that are vowels.
Compile and run your program with the following verses (English and Maori) from a popular New Zealand song. Enter one line at a time, and total the vowel counts by hand for the entire verse. Note than Polynesian languages tend to use a high percentage of vowels.

(a)    English version
Now is the hour, when we must say goodbye.
Soon you’ll be sailing far across the sea.
While you’re away, oh, then, remember me.
When you return, you’ll find me waiting here.

(b)    Maori version
Po atarau, E moea iho nei
E haere ana, Koe ki pamamao
Haere ra, Ka hoki mai ano
Ki i te tau, E tangi atu nei

3.    Encryption string class:  Write a Python class called EString to implement a class of strings that can be encrypted and decrypted. The class has two private member variables:
(a)    a string W that represents the string to be encrypted or decrypted.
(b)    an integer variable K that is the key to be used to encrypt or decrypt the string.

Write code for the following public methods in this class:
(1)    A constructor EString(W) that creates an EString object S and sets the first member variable to the lower-case version of string W. The initial key value is set to 0.
(2)    A S.setKey(K) method that sets the key value to integer K (-25 <= K <= 25).
(3)    A S.encrypt() method that returns an encrypted version of string S, using the current value of key K. The encryption algorithm is a Caesar cipher with shift K. Only lower-case letters are encrypted by the cipher. Note: This method can be used to both encrypt (using K) and decrypt (using –K).

4.    Encryption strings:  Write a Python test program that demonstrates the methods of the EString class. Input a string W and a key value K. Then create an EString object S1.
Perform the following encryption and decryption activities:
(1)    Encrypt your string using key K. Call the resulting string S2.
(2)    Then decrypt string S2 using key – K. Call the result S3.
Your test program should then output strings S2 and S3.
Run your test program for the following cases, and display the results.
(a)    W = Have a nice day!                            K =  3
(b)    W = I love dark chocolate.                    K =  5
(c)    W = My password is “meatballs”.            K = -8
(d)    W = We attack the enemy at dawn!            K = 17

PLACE THIS ORDER OR A SIMILAR ORDER WITH US TODAY AND GET AN AMAZING DISCOUNT 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *