1 / 29

An Introduction to Programming with C++ Fifth Edition

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

nathan
Télécharger la présentation

An Introduction to Programming with C++ Fifth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Introduction to Programming with C++Fifth Edition Chapter 12 String Manipulation

  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 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++ 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 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 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 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() An Introduction to Programming with C++, Fifth Edition

  8. Determining the Number of Characters Contained in a String (continued) An Introduction to Programming with C++, Fifth Edition

  9. Determining the Number of Characters Contained in a String (continued) 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 An Introduction to Programming with C++, Fifth Edition

  11. Removing Characters from a String (continued) 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” An Introduction to Programming with C++, Fifth Edition

  13. Accessing Characters Contained in a String (continued) An Introduction to Programming with C++, Fifth Edition

  14. Accessing Characters Contained in a String (continued) 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 An Introduction to Programming with C++, Fifth Edition

  16. Replacing Characters in a String (continued) An Introduction to Programming with C++, Fifth Edition

  17. Inserting Characters within a String • insert()inserts characters within a string 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 An Introduction to Programming with C++, Fifth Edition

  19. Searching a String (continued) 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() An Introduction to Programming with C++, Fifth Edition

  21. Comparing a Portion of a String with Another String (continued) An Introduction to Programming with C++, Fifth Edition

  22. Comparing a Portion of a String with Another String (continued) 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 An Introduction to Programming with C++, Fifth Edition

  24. Duplicating a Character within a String Variable (continued) 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 An Introduction to Programming with C++, Fifth Edition

  26. Concatenating Strings (continued) It is easier to use: hyphens.assign(5, '-'); An Introduction to Programming with C++, Fifth Edition

  27. Summary • String manipulation is a common programming task An Introduction to Programming with C++, Fifth Edition

  28. Summary (continued) 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 An Introduction to Programming with C++, Fifth Edition

More Related