CS 1510 String Lab


Once again, this week I will ask you to work with a partner of your choice.  You will turn in a SINGLE answer sheet for both partners.  As a reminder, you should...

Failure to follow these guidelines may cause you to lose points for this activity.


Activity A : Introduction to Strings

Yesterday in class we spent a little bit of time looking at String as a data structure and some of the initial things you can do to the String. Let's review:

At the command prompt type

>>> greet = "Have a great day!"
[Q1] What happens when you invoke each of these commands:  (be careful with the last one)
Command Results
len(greet)  
greet[1]                                                                 
greet[7]  
greet[1:8]  
greet[:8]  
greet[8:]  
greet[13]+greet[10:12]+greet[14]  
greet[0]*3  
(greet[0]+greet[5:7])*3  

 

[Q2] What commands would you type (using greet from above) to get the following results

Command Results
                                                                             t                 
  !
  the last letter of ANY message (While you may use subtraction, you should NOT use the negative operators, like greet[-1])
  great da
  great great dada
  yard

 

 

[Q3] What happens when you invoke each of these commands: 

Command Results
greet[-1]                                                                 
greet[-4]  
greet[-11]  
greet[-7:-3]  

 

[Q4]  What is happening when you use negative numbers in the indexing/slice command? 

 


Activity B : Let's do something useful

Suppose that every UNI email address was generated from the following formula:

The first 5 characters of your last name followed by the first initial of your first name followed by @uni.edu

For example:

Write a program called generateEmail.py   This program should prompt the user for the first name and then prompt the user for their last name.  It should then print that person's email address using the formula described above.


[SIG1] Please show this to an instructor BEFORE moving on

Activity C : A String activity

Often times we want to write code that loops through a string.  There are several ways to do this.

Enter the text below into an editor window.

Run the code.  What happens?

 

 

Now try this related, but different block of code

Run this code.  What happens?

 

The assignment

Using either of the blocks of code above as a starting point, write a program called reverseString.py which takes in a String of text entered by the user and prints out the exact same string but in reverse.  For example:

 

PLEASE NOTE, I am asking you to write code to actually build the reversed string.  You should not use the "cheat" (legal, but not the point) that your book discusses to do this with only one command.


[SIG2] Please show this to an instructor BEFORE moving on

Activity D : Another String activity

Write a program called countVowels.py which takes in a string of text entered by the user and uses a for loop to iterate over each character in that string to count the number of EACH VOWEL (a, e, i, o , and u).  The program should then print the results of this count.  For example

(Note that I am asking YOU to count in your code and  not use one of the built in tools in the language which you will look at later in this lab).

SUGGESTION - Begin by figuring out how to make this work to count As.  When you are done, add in functionality for the other vowels.

NOTE: Again, I am asking YOU to do this, not use the built in functions of a string.


[SIG3] Please show this to an instructor BEFORE moving on

Final Submission

Please submit your code for electronic grading using the eLearning submission system. 

Follow the directions on the system to select the appropriate course and assignment and submit

Don't forget to hand in your answer sheet to the professor or TA before you leave!