Home / working with strings in c / CS 1110, LAB 3: WORKING WITH STRINGS

CS 1110, LAB 3: WORKING WITH STRINGS - working with strings in c


CS 1110, LAB 3: WORKING WITH STRINGS-working with strings in c

CS 1110, LAB 3: WORKING WITH STRINGS
http://www.cs.cornell.edu/courses/cs1110/2016fa/labs/lab03/
First Name: Last Name: NetID:
The purpose of this lab is to help you to better understand strings and user-defined functions.
One of your primary tasks in Assignment 1 will be writing functions that take a string as argument
and return a string when done. This assignment will go a long way in helping you understand how
to work on Assignment 1.
This lab will be similar in style to the previous lab. There are several tables that you will need
to fill in the values for. These tables will help you understand string methods and expressions. In
addition, we will ask you to create another module. This module will be a bit more involved than
the one from last time.
In the past, this lab has been one of the longest of the entire course. We have moved things about
this semester, giving you a week longer on Assignment 1, allowing us to spread important concepts
between this lab and the next. Therefore, we hope that this lab is more manageable. With that
said, if you have never programmed before, you may find the last part of this lab to be significantly
harder than any of the previous labs. If you find yourself struggling, we strongly encourage you to
sign up for one of the One-on-Ones announced in class.
Getting Credit for the Lab. Once again, you have a choice between getting credit through the
online system or your instructor. The online lab is available at the web page
http://www.cs.cornell.edu/courses/cs1110/2016fa/labs/lab03/
The advantage of the online system is that you can do it on your own time and verify that you
got credit. The disadvantage is that your answers must be correct. If you want more guided help
on this lab, you should use the worksheet instead. Despite the demands of the online system, labs
are graded on effort, not correctness.
If you use this worksheet, your answer will include both a sheet of paper (or the sheet provided
to you in lab) and file called lab03.py. This is a file that you will create frome scratch. When you
are finished, you should show both this file and your written answers to your lab instructor, who
will record that you did it.
This lab is long enough that you may not finish during class time. If you do not finish, you have
until the beginning of lab next week to finish it. Over the next week, you may either (1)
complete the lab online, (2) show your lab to a consultant during consulting hours, or (3) show your
lab to an instructor at the beginning of the next lab session. The online version will stop receiving
submissions after the last lab on Wednesday.
Course authors: D. Gries, L. Lee, S. Marschner, W. White
1
1. String Expressions
This section should be very familiar to you by now. The first table challenges you to evaluate an
expression. The second table asks you to "fill in the blank", completing an expression to give you
the provided value. The blanks will all either be a string or number literal.
Throughout this section, pay close attention to spaces and to the different types of quotation
marks being used. We use both ' (single quote) and " (double quote). While both work, there are
reasons to use one over another. In addition, there is a (back quote). While we did not the see
the back quote in class, you should be able to figure it out from this lab.
Expression Expected Calculated Expression Calculated Literal in
Value Value Value the Box
'CS '+'is '+'fun' 'Hello '+ 'Hello World'
'CS'+'is'+'fun' 'Hello'+ 'Hello World'
"CS "+"is "+"fun" "Hello "+ 'Hello World'
"CS "+('is '+"fun") "A"+( +"C") 'ABC'
'Double "' 'A'+ +'B' 'A"B'
"Single '" 'A'+ +'B' "A'B"
'Single '' 'A'+ +'C' "A'B\"C"
'' + 'ok' '' + ''
'' + '4 / 2' '' + '2+2'
'' + 4 / 2 '4' + '4 / 2'
'' + str(4 / 2) str( + 2) '4'
'' + 4 / 2 * 3 '12'
2
2. String Methods
Strings have many handy methods, whose specifications can be found at the following URL:
http://docs.python.org/2/library/stdtypes.html#string-methods
For right now, look at section 5.6.1 "String Methods," and do not worry that about all the unfamiliar
terminology. You will understand it all by the end of the semester.
Using a method is a lot like using a function. The difference is that you first start with the string
to operate on, follow it with a period, and then use the name of the method as in a function call.
For example, the following all work in Python:
s.index('a') # assuming the variable s contains a string
'CS 1110'.index('1') # you can call methods on a literal value
s.strip().index('a') # s.strip() returns a string, which takes a method
Before starting with the table below, enter the following statement into the Python shell:
s = 'Hello World!'
Once you have done that, use the string stored in s to fill out the tables below, just as before.
Expression Expected Calculated Expression Calculated Literal in
Value Value Value the Box
s[1] s[ ] 'W'
s[15] [2] 'C'
s[1:5] s[4: ] 'o Wo'
s[:5] s[: ] 'He'
s[5:] s[ :] 'rld'
'e' in s in 'ABC' True
'x' in s 'e' in False
s.index('e') s.index( ) 4
s.index('x') .index('x') 2
s.index('l',5) s.index('o', ) 7
s.find('e') s.find( ) 4
s.find('x') .find('e') -1
s.count('o') s.count( ) 3
3

How to write string in C? In C programming, a string is a sequence of characters terminated with a null character 0. For example: char c [] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character 0 at the end by default. Memory Diagram.