290 likes | 374 Views
An Introduction to Programming with C++ Fifth Edition. Chapter 12 String Manipulation. 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
E N D
An Introduction to Programming with C++Fifth Edition Chapter 12 String Manipulation
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 An Introduction to Programming with C++, Fifth Edition
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++ An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
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() An Introduction to Programming with C++, Fifth Edition
Determining the Number of Characters Contained in a String (continued) An Introduction to Programming with C++, Fifth Edition
Determining the Number of Characters Contained in a String (continued) An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
Removing Characters from a String (continued) An Introduction to Programming with C++, Fifth Edition
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” An Introduction to Programming with C++, Fifth Edition
Accessing Characters Contained in a String (continued) An Introduction to Programming with C++, Fifth Edition
Accessing Characters Contained in a String (continued) An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition
Replacing Characters in a String (continued) An Introduction to Programming with C++, Fifth Edition
Inserting Characters within a String • insert()inserts characters within a string An Introduction to Programming with C++, Fifth Edition
Searching a String • Use find()to search a string variable to determine if it contains a sequence of characters An Introduction to Programming with C++, Fifth Edition
Searching a String (continued) An Introduction to Programming with C++, Fifth Edition
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() An Introduction to Programming with C++, Fifth Edition
Comparing a Portion of a String with Another String (continued) An Introduction to Programming with C++, Fifth Edition
Comparing a Portion of a String with Another String (continued) An Introduction to Programming with C++, Fifth Edition
Duplicating a Character within a String Variable • Use assign()to duplicate a character a specified number of times An Introduction to Programming with C++, Fifth Edition
Duplicating a Character within a String Variable (continued) An Introduction to Programming with C++, Fifth Edition
Concatenating Strings • Connecting (or linking) strings together is called concatenating • In C++, you concatenate strings using the concatenation operator • The + sign An Introduction to Programming with C++, Fifth Edition
Concatenating Strings (continued) It is easier to use: hyphens.assign(5, '-'); An Introduction to Programming with C++, Fifth Edition
Summary • String manipulation is a common programming task An Introduction to Programming with C++, Fifth Edition
Summary (continued) An Introduction to Programming with C++, Fifth Edition
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 An Introduction to Programming with C++, Fifth Edition