Week05 Lab05 StringProcessing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Laboratory 5 Strings

Introduction to Computer Science


COMP 112
Wesleyan University
Prof. Kelly M. Thayer
[email protected]

Introduction
So far we have considered strings as a whole word. However, they can be treated as separate
characters, indexed, and processed. We will be examining the various ways strings can be manipulated.
One may also do some task for each element in a list; for loops are especially useful for this task. In
this lab we will work examples to gain familiarity with strings and lists, build on use of functions, explore
mutability, and use for loops to process each item of a loop.

Learning Objectives.
Upon completing this lab, students should be able to:

 Make diagrams of strings to show the numbering


 Slice strings
 Use the upper and lower method to standardize input with respect to capitalization
 Combine string manipulation with flow control

Part I. String Indexing


For each of the following examples, write a diagram showing the indexing of the string. Then indicate
what will be returned . Then try it with Python to check.

Example of indexing of the string ‘hello!’

1. river = ‘Mississippi’
a. river [2] _____
b. river [4] _____
c. river [9] _____
2. river = ’Connecticut’
a. river [2] _____
b. river [4] _____

1
c. river [1] _____
d. river [3] _____

3. river = ‘Salmon’
a. river [-6] _____
b. river [-2] _____
c. river [-1] _____

Part II. String Slicing.


The slice operator can be used to return a substring. The syntax is

<string> [<strart_index> : <stop_index>]

For the following problems, write the indices of the string as above, and apply the slice operator. Try to
figure it out, and then test it with Python to check your answer.

1. river = ‘Missouri’
a. river [3 : 7] ________

2. river = ‘Rio Grande’


a. river [4 : 8] _______
b. river [4 : 9] _______
c. river [3 : 9] _______

3. river = ‘Yellowstone’
a. river [ :6] _____
b. river [7: ] _____
c. river [6: ] _____

Part III. String Methods Upper and Lower


The methods <string>.upper() and <string>.lower change the case of the input. For the following
examples, guess the output and test with Python.

1. comment = ‘Stop annoying me!’


a. comment.upper() _____________________________
b. (comment.upper() ) .lower() ____________________________
2. Word = ‘CaMeLcAsE’
a. word.upper() ____________
b. word.lower()_____________

2
Part IV. Converting to and From Unicode
Python has two built in functions ord() and chr() which will translate words into and out of
the Unicode (numeric) representation of the character. Since they are recorded in order, this can be
useful for indexing over the letters. Note that ord() takes strings, whereas chr() takes integers.

1. Get the Unicode equivalent for the following letters using the ord()function.
a. A g. a
b. B h. b
c. C i. c
d. X j. x
e. Y k. y
f. Z l. z
2. Show that the Unicode representation can be decoded back to its letter representation using
chr().
3. What is the range of the capital and lower case letters?

Part V. String Processing Application: Program Reverse String


To the right is a program based on the reverse
function. Take a moment to type in the program and
run it.

1. What does len(s) do?


_____________________________________
2. What is the role of the variable acc?
_____________________________________
3. What does the function reverse take for
input? _______________________________
4. What would happen if the line i += 1 were
deleted?
_____________________________________
5. Explain in your own words how this program
reverses the order of the letters in the user
supplied word.
______________________________________________________________________________
______________________________________________________________________________
6. While the program is executing the function, in which variable is the reversed word stored?
When the function execution completes, to what variable is this information passed and how?
______________________________________________________________________________
______________________________________________________________________________

3
Additional Exercises.
Exercise 1. Word within a Word. Many longer words often contain letters that can be used to make
shorter words. For example, in part B, words within a compound word were extracted. Pick a long
word, and using the results of at least three slices to make a new word. Then, give your word and slices
to your partner and try to figure out each other’s word.

Exercise 2. Reverse Minus. The function reverse (Part V above) finds the reverse of a string using an
accumulator as the loop moves over a string starting with the first character. Put the code in a program
and modify it so that it instead reverses by processing the last character first to reverse the string.

Exercise 3. Caesar Cipher Cryptography. A Caesar


Cipher is a kind of encoding involving the mapping of the
letters of the alphabet to the letter some fixed number of
positions forward or backward. For example, ABC would
move to BCD if the offset were 1. Thus, figuring out the
key to such a cipher entails figuring out what the offset
is. Computers can effectively be used to decode ciphers
by testing out the possible offsets, and the correct one is
the one which generates decipherable text. A sample is
depicted as a wheel .

Working with your partner, write a program that


operates both in the encoding and decoding mode. In the
encoding mode, it should take user input and the offset
value to produce an encoded message. In the decoding
mode, it should take text and decode it trying all possible
offsets. Demonstrate it works by trading an encoded
message with another group.

You might also like