Download presentation
Presentation is loading. Please wait.
1
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation
2
An Introduction to Programming with C++, Fifth Edition2 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
3
An Introduction to Programming with C++, Fifth Edition3 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++
4
An Introduction to Programming with C++, Fifth Edition4 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
5
An Introduction to Programming with C++, Fifth Edition5 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
6
An Introduction to Programming with C++, Fifth Edition6 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
7
An Introduction to Programming with C++, Fifth Edition7 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()
8
An Introduction to Programming with C++, Fifth Edition8 Determining the Number of Characters Contained in a String (continued)
9
An Introduction to Programming with C++, Fifth Edition9 Determining the Number of Characters Contained in a String (continued)
10
An Introduction to Programming with C++, Fifth Edition10 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
11
An Introduction to Programming with C++, Fifth Edition11 Removing Characters from a String (continued)
12
An Introduction to Programming with C++, Fifth Edition12 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”
13
An Introduction to Programming with C++, Fifth Edition13 Accessing Characters Contained in a String (continued)
14
An Introduction to Programming with C++, Fifth Edition14 Accessing Characters Contained in a String (continued)
15
An Introduction to Programming with C++, Fifth Edition15 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
16
An Introduction to Programming with C++, Fifth Edition16 Replacing Characters in a String (continued)
17
An Introduction to Programming with C++, Fifth Edition17 Inserting Characters within a String insert() inserts characters within a string
18
An Introduction to Programming with C++, Fifth Edition18 Searching a String Use find() to search a string variable to determine if it contains a sequence of characters
19
An Introduction to Programming with C++, Fifth Edition19 Searching a String (continued)
20
An Introduction to Programming with C++, Fifth Edition20 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()
21
An Introduction to Programming with C++, Fifth Edition21 Comparing a Portion of a String with Another String (continued)
22
An Introduction to Programming with C++, Fifth Edition22 Comparing a Portion of a String with Another String (continued)
23
An Introduction to Programming with C++, Fifth Edition23 Duplicating a Character within a String Variable Use assign() to duplicate a character a specified number of times
24
An Introduction to Programming with C++, Fifth Edition24 Duplicating a Character within a String Variable (continued)
25
An Introduction to Programming with C++, Fifth Edition25 Concatenating Strings Connecting (or linking) strings together is called concatenating In C++, you concatenate strings using the concatenation operator –The + sign
26
An Introduction to Programming with C++, Fifth Edition26 It is easier to use: hyphens.assign(5, '-'); Concatenating Strings (continued)
27
An Introduction to Programming with C++, Fifth Edition27 Summary String manipulation is a common programming task
28
An Introduction to Programming with C++, Fifth Edition28 Summary (continued)
29
An Introduction to Programming with C++, Fifth Edition29 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.