An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
-
date post
19-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
![Page 1: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/1.jpg)
An Introduction to Programming with C++
Fifth Edition
Chapter 12String Manipulation
![Page 2: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/2.jpg)
An Introduction to Programming with C++, Fifth Edition 2
Objectives
• Determine the number of characters contained in a string
• Remove characters from a string
• Access characters contained in a string
• Replace characters in a string
• Insert characters within a string
![Page 3: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/3.jpg)
An Introduction to Programming with C++, Fifth Edition 3
Objectives (continued)
• Search a string for another string
• Compare a portion of a string variable’s contents to another string
• Duplicate a character within a string variable
• Concatenate strings
• Perform string manipulation in .NET C++
![Page 4: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/4.jpg)
An Introduction to Programming with C++, Fifth Edition 4
Concept Lesson
• Manipulating Strings
• Determining the Number of Characters Contained in a String
• Removing Characters from a String
• Accessing Characters Contained in a String
• Replacing Characters in a String
![Page 5: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/5.jpg)
An Introduction to Programming with C++, Fifth Edition 5
Concept Lesson (continued)
• Inserting Characters within a String
• Searching a String
• Comparing a Portion of a String with Another String
• Duplicating a Character within a String Variable
• Concatenating Strings
![Page 6: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/6.jpg)
An Introduction to Programming with C++, Fifth Edition 6
Manipulating Strings
• Programs often need to manipulate string data– For example
• Verify that an inventory part number begins with a specific letter
• Determine whether the last three characters in an employee number are valid
![Page 7: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/7.jpg)
An Introduction to Programming with C++, Fifth Edition 7
Determining the Number of Characters Contained in a String
• Use length() to determine the number of characters in a string variable– Or use size()
• Syntax: string.size()
![Page 8: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/8.jpg)
An Introduction to Programming with C++, Fifth Edition 8
Determining the Number of Characters Contained in a String (continued)
![Page 9: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/9.jpg)
An Introduction to Programming with C++, Fifth Edition 9
Determining the Number of Characters Contained in a String (continued)
![Page 10: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/10.jpg)
An Introduction to Programming with C++, Fifth Edition 10
Removing Characters from a String
• Use erase() to remove one or more characters located anywhere in a string variable– Syntax in Figure 12-2
• subscript and count arguments can be numeric literal constants or the names of numeric variables
![Page 11: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/11.jpg)
An Introduction to Programming with C++, Fifth Edition 11
Removing Characters from a String (continued)
![Page 12: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/12.jpg)
An Introduction to Programming with C++, Fifth Edition 12
Accessing Characters Contained in a String
• You may need to determine whether a specific letter appears as the third character in a string
• Or, you may need to display only the string’s first five characters
• Use substr() to access any number of characters contained in a string variable– “substr” stands for “substring”
![Page 13: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/13.jpg)
An Introduction to Programming with C++, Fifth Edition 13
Accessing Characters Contained in a String (continued)
![Page 14: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/14.jpg)
An Introduction to Programming with C++, Fifth Edition 14
Accessing Characters Contained in a String (continued)
![Page 15: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/15.jpg)
An Introduction to Programming with C++, Fifth Edition 15
Replacing Characters in a String
• Use replace() to replace a sequence of characters in a string variable with another sequence of characters– Syntax in Figure 12-4
• replacementString can be a string literal constant or the name of a string variable
![Page 16: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/16.jpg)
An Introduction to Programming with C++, Fifth Edition 16
Replacing Characters in a String (continued)
![Page 17: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/17.jpg)
An Introduction to Programming with C++, Fifth Edition 17
Inserting Characters within a String
• insert() inserts characters within a string
![Page 18: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/18.jpg)
An Introduction to Programming with C++, Fifth Edition 18
Searching a String
• Use find() to search a string variable to determine if it contains a sequence of characters
![Page 19: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/19.jpg)
An Introduction to Programming with C++, Fifth Edition 19
Searching a String (continued)
![Page 20: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/20.jpg)
An Introduction to Programming with C++, Fifth Edition 20
Comparing a Portion of a String with Another String
• Use the comparison operators to compare strings– >, >=, <, <=, ==, and !=– E.g., while (name != "DONE")
• To compare a portion of one string with another string, use compare()
![Page 21: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/21.jpg)
An Introduction to Programming with C++, Fifth Edition 21
Comparing a Portion of a String with Another String (continued)
![Page 22: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/22.jpg)
An Introduction to Programming with C++, Fifth Edition 22
Comparing a Portion of a String with Another String (continued)
![Page 23: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/23.jpg)
An Introduction to Programming with C++, Fifth Edition 23
Duplicating a Character within a String Variable
• Use assign() to duplicate a character a specified number of times
![Page 24: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/24.jpg)
An Introduction to Programming with C++, Fifth Edition 24
Duplicating a Character within a String Variable (continued)
![Page 25: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/25.jpg)
An Introduction to Programming with C++, Fifth Edition 25
Concatenating Strings
• Connecting (or linking) strings together is called concatenating
• In C++, you concatenate strings using the concatenation operator– The + sign
![Page 26: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/26.jpg)
An Introduction to Programming with C++, Fifth Edition 26
It is easier to use:hyphens.assign(5, '-');
Concatenating Strings (continued)
![Page 27: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/27.jpg)
An Introduction to Programming with C++, Fifth Edition 27
Summary
• String manipulation is a common programming task
![Page 28: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/28.jpg)
An Introduction to Programming with C++, Fifth Edition 28
Summary (continued)
![Page 29: An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.](https://reader038.fdocuments.net/reader038/viewer/2022103123/56649d375503460f94a0f26b/html5/thumbnails/29.jpg)
An Introduction to Programming with C++, Fifth Edition 29
Application Lesson: Using String Manipulation in a C++ Program
• Lab 12.1: Stop and Analyze
• Lab 12.2– Program that allows two students to play a simplified
version of the Hangman Game on the computer
• Lab 12.3– Modify program so that it allows player 1 to enter a
word of any length
• Lab 12.4: Desk-Check Lab
• Lab 12.5: Debugging Lab