Professional Documents
Culture Documents
Object Oriented Programming: Spring 2018
Object Oriented Programming: Spring 2018
Spring 2018
Lecture 1: Strings
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 1
Objectives
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 2
Objectives (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 3
The string Data Type
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 4
The string Data Type (cont’d.)
Figure 13-1 How to declare and initialize string variables and named constants
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 5
Getting String Input from the Keyboard
• In previous chapters the extraction operator (>>) was used to get numbers
and characters from the user at the keyboard.
• The extraction operator can also be used to get string input from the
keyboard.
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 6
Getting String Input from the Keyboard (cont’d.)
Figure 13-2 How to use the extraction operator (>>) to get string input from the
keyboard
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 7
Getting String Input from the Keyboard (cont’d.)
• The getline function obtains string data from the keyboard and stores it
in a string variable
• Syntax is:
getline(cin, stringVariableName
[, delimiterCharacter]);
• Three actual arguments (first two required):
– cin argument refers to computer keyboard
– stringVariableName argument is name of a string variable in which to store
input
– Optional delimiterCharacter indicates character that immediately follows last
character of string
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 8
Getting String Input from the Keyboard (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 9
Getting String Input from the Keyboard (cont’d.)
Figure 13-3 How to use the getline function to get string input from the keyboard
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 10
Primrose Auction House Program
Figure 13-4 Problem specification for the Primrose Auction House Program
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 11
Primrose Auction House Program (cont’d.)
Figure 13-4 IPO chart for the Primrose Auction House Program
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 12
Primrose Auction House Program (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 13
Primrose Auction House Program (cont’d.)
Figure 13-6 Modified Primrose Auction House Program and sample run
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 14
The ignore Function
• The ignore function instructs the computer to read and ignore characters
stored in the cin object by consuming (discarding) them
• Syntax is:
– cin.ignore([numberOfCharacters][, delimiterCharacter]);
• Has two actual arguments, both optional:
– numberOfCharacters argument is maximum number of characters function
should consume (default is 1)
– delimiterCharacter argument stops ignore function from reading and
discarding any more characters when consumed
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 15
The ignore Function (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 16
The ignore Function (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 17
The ignore Function (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 18
The ignore Function (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 19
Determining the Number of
Characters in a string Variable
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 20
Determining the Number of Characters
Contained in a string Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 21
Determining the Number of Characters Contained in a
string Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 22
Determining the Number of Characters Contained in a
string Variable (cont’d.)
Figure 13-11 Remainder of the ZIP Code Program and sample run
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 23
Accessing the Characters Contained
in a string Variable
• The substr function allows you to access any number of characters
contained in a string variable by returning the specified characters
• Syntax is:
– string.substr(subscript[, count])
• Has two arguments (first is required):
– subscript argument represents subscript of first character you want to access in
string
– count argument is number of characters to return after character specified by
subscript
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 24
Accessing the Characters Contained in a string
Variable (cont’d.)
• If you omit count argument, function returns all characters from
subscript position through end of string
• A string is equivalent to a one-dimensional array of characters
• Each character has a unique subscript that indicates character’s position in
the string
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 25
Accessing the Characters Contained in a string
Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 26
Accessing the Characters Contained in a string
Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 27
Accessing the Characters Contained in a string
Variable (cont’d.)
Figure 13-13 Remainder of the Modified ZIP Code Program and sample run
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 28
Searching the Contents of a string Variable
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 29
Searching the Contents of a string Variable
(cont’d.)
• find function performs a case-sensitive search (uppercase and lowercase
letters are not equivalent)
– When searchString is contained within string, function returns an integer that
indicates beginning position of searchString within string
– Function returns -1 when searchString is not contained within string
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 30
Searching the Contents of a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 31
Searching the Contents of a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 32
Searching the Contents of a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 33
Searching the Contents of a string Variable
(cont’d.)
Figure 13-15 Remainder of the Rearrange Name Program and sample run
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 34
Removing Characters from a string Variable
• You can use the erase function to remove one or more characters from a
string variable
• Syntax is:
– string.erase(subscript[, count]);
– subscript is position of first character you want to remove
– Optional count argument is an integer that specifies number of characters you
want removed
– If you omit count, function removes all characters from subscript through end
of string
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 35
Removing Characters from a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 36
Removing Characters from a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 37
Removing Characters from a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 38
Replacing Characters in a string Variable
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 39
Replacing Characters in a string Variable
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 40
Replacing Characters in a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 41
Inserting Characters within a string Variable
• You can use the insert function to insert characters into a string
variable
• Syntax is:
– string.insert(subscript, insertString);
– subscript specifies where in string you want characters inserted
– insertString specifies characters to be inserted
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 42
Inserting Characters within a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 43
Inserting Characters within a string Variable
(cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 44
Duplicating a Character within a string Variable
• You can use the assign function to duplicate one character a specified
number of times and assign the resulting string to a string variable
• Syntax is:
– string.assign(count, character);
– count argument is an integer that indicates the number of times you want to
duplicate the character specified in character argument
– character argument can be either a character literal constant or a char
memory location
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 45
Duplicating a Character within a
string Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 46
Duplicating a Character within a
string Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 47
Duplicating a Character within a
string Variable (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 48
Concatenating Strings
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 49
Concatenating Strings (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 50
Concatenating Strings (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 51
Summary
• The string data type was added to the C++ language using the string
class
• Memory locations whose data type is string are initialized using string
literal constants (0 or more characters enclosed in double quotation marks)
• You can use the extraction operator to get a string from the user at the
keyboard if the string does not contain a white-space character
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 52
Summary (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 53
Summary (cont’d.)
• The computer stores characters entered at the keyboard in the cin object
• Both the extraction operator and the getline function remove characters
from cin
• The extraction operator leaves the newline character in cin, while the
getline function consumes the newline character
• The ignore function reads and then consumes characters entered at the
keyboard
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 54
Summary (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 55
Lab 13-1: Stop and Analyze
• Study the code in Figure 13-27 and then answer the questions.
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 56
Lab 13-1: Stop and Analyze (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 57
Lab 13-2: Plan and Create
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 58
Lab 13-3: Modify
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 59
Lab 13-4: What’s Missing?
• The program is this lab should include commas (if necessary) when
displaying the output.
• Follow the instructions for starting C++ and opening the Lab13-4.cpp file.
Put the C++ instructions in the proper order, and then determine the one or
more missing instructions.
• Test the program appropriately.
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 60
Lab 13-5: Desk-Check
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 61
Lab 13-5: Desk-Check (cont’d.)
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 62
Lab 13-6: Debug
Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 63