You are on page 1of 63

Object Oriented Programming

Spring 2018

Lecture 1: Strings

Dr. Bakhtiar Kasi


20-Mar-18

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 1
Objectives

• Declare string variables and named constants


• Get string input using the getline function
• Ignore characters using the ignore function
• Determine the number of characters in a string
• Access the characters in a string
• Search a string

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 2
Objectives (cont’d.)

• Remove characters from a string


• Convert a string to a numeric data type
• Replace characters in a string
• Insert characters within a string
• Duplicate characters within a string
• Concatenate strings

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 3
The string Data Type

• The string data type is not a fundamental data type in C++


• Added to C++ through use of string class
• To use the string class, a program must contain the #include
<string> directive
• You can use the string class to create string variables or string
named constants
• Memory locations of type string are initialized with string literal
constants—0 or more characters enclosed in double quotation marks (“”)

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.)

• Function will continue to read characters entered at keyboard until it


encounters a delimiter character
• Default delimiter character is newline character
• When the function encounters a delimiter character, it discards the
character—process called consuming the character
• Newline character is designated by ‘\n’
• Backslash is called an escape character
• Backslash and character following it are called an escape sequence

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.)

Figure 13-5 Primrose Auction House Program and sample run

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.)

Figure 13-7 How to use the ignore function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 16
The ignore Function (cont’d.)

Figure 13-7 How to use the ignore function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 17
The ignore Function (cont’d.)

Figure 13-8 Beginning of the Modified Primrose Auction House Program


with the ignore function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 18
The ignore Function (cont’d.)

Figure 13-8 Remainder of the Modified Primrose Auction House Program


with the ignore function and sample run

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 19
Determining the Number of
Characters in a string Variable

• You use string class’s length function to


determine the number of characters in a string
variable
• Syntax is:
– string.length()
• Returns number of characters contained in string

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.)

Figure 13-10 How to use the length function

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.)

Figure 13-11 Beginning of the ZIP Code Program

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.)

Figure 13-12 How to use the substr function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 26
Accessing the Characters Contained in a string
Variable (cont’d.)

Figure 13-13 Beginning of the Modified ZIP Code Program

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

• You use the find function to search contents of a string variable to


determine whether it contains a specific sequence of characters
• Syntax is:
– string.find(searchString, subscript)
– searchString argument is a string for which you are searching within string
– subscript argument specifies starting position for the search

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.)

Figure 13-14 How to use the find function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 31
Searching the Contents of a string Variable
(cont’d.)

Figure 13-14 How to use the find function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 32
Searching the Contents of a string Variable
(cont’d.)

Figure 13-15 Beginning of the Rearrange Name Program

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.)

Figure 13-17 How to use the erase function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 36
Removing Characters from a string Variable
(cont’d.)

Figure 13-17 How to use the erase function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 37
Removing Characters from a string Variable
(cont’d.)

Figure 13-18 Bonus Program and sample run

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 38
Replacing Characters in a string Variable

• The replace function replaces one sequence of characters in a string


variable with another
• Syntax is:
– string.replace(subscript, count, replacementString);
– subscript argument specifies where to begin replacing characters in string
– count argument indicates number of characters to replace
– replacementString argument contains replacement characters

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 39
Replacing Characters in a string Variable

Figure 13-19 How to use the replace function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 40
Replacing Characters in a string Variable
(cont’d.)

Figure 13-20 Partial Bonus Program showing the replace function

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.)

Figure 13-21 How to use the insert function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 43
Inserting Characters within a string Variable
(cont’d.)

Figure 13-22 Social Security Number Program and sample run

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.)

Figure 13-23 How to use the assign function

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 46
Duplicating a Character within a
string Variable (cont’d.)

Figure 13-24 Company Name Program

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 47
Duplicating a Character within a
string Variable (cont’d.)

Figure 13-24 Sample run of the Company Name Program

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 48
Concatenating Strings

• String concatenation refers to the process of connecting (linking) strings


together
• You concatenate strings using the concatenation operator (+ sign)

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 49
Concatenating Strings (cont’d.)

Figure 13-25 How to use the concatenation operator

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 50
Concatenating Strings (cont’d.)

Figure 13-26 Partial Company Name Program showing string concatenation

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.)

• The getline function gets a string of characters entered at the keyboard


and stores them in a string variable
• The string can contain any characters, including white-space characters
• The getline function reads and stores characters from the keyboard until
it encounters a delimiter character, which it consumes (discards)
• The default delimiter character is the newline character

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.)

• The ignore function stops reading and consuming characters when it


consumes either a specified number of characters or a delimiter character
• The default number of characters to consume is 1
• The assign, erase, insert, and replace functions are self-contained
statements that change the value of a string variable
• The concatenation operator (+) is used to join (link) strings together

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.)

Figure 13-27 Code for Lab 13-1

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 57
Lab 13-2: Plan and Create

Figure 13-29 Problem specification for Lab 13-2

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 58
Lab 13-3: Modify

• Modify the program in Lab13-2.cpp. Currently, the program allows player 1


to enter only a five-character word. Modify the program so that player 1
can enter a word of any length. Save the modified program as Lab13-3.cpp
• Save, run, and test the program.

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

• Desk-check the code in Figure 13-33.


• What will the code display on the screen?

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 61
Lab 13-5: Desk-Check (cont’d.)

Figure 13-33 Code for Lab 13-5

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 62
Lab 13-6: Debug

• Run the program in the Lab13-6.cpp file


• Type Joe and press Enter.
• Rather than displaying the letters J, o, and e on three separate lines, the
program displays Joe, oe, and e.
• Debug the program.

Dr. Bakhtiar Kasi (BUITEMS) Spring 2018 : Object Oriented Programming 3/20/18 63

You might also like