An Introduction to Programming with C++ Fifth Edition

of 29
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Documents

Published:

Views: 39 | Pages: 29

Extension: PDF | Download: 0

Share
Description
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
Transcript
An Introduction to Programming with C++Fifth EditionChapter 12String ManipulationObjectives
  • 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 EditionObjectives (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 EditionConcept 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 EditionConcept 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 EditionManipulating 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 EditionDetermining 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 EditionDetermining the Number of Characters Contained in a String (continued)An Introduction to Programming with C++, Fifth EditionDetermining the Number of Characters Contained in a String (continued)An Introduction to Programming with C++, Fifth EditionRemoving 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 EditionRemoving Characters from a String (continued)An Introduction to Programming with C++, Fifth EditionAccessing 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 EditionAccessing Characters Contained in a String (continued)An Introduction to Programming with C++, Fifth EditionAccessing Characters Contained in a String (continued)An Introduction to Programming with C++, Fifth EditionReplacing 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 EditionReplacing Characters in a String (continued)An Introduction to Programming with C++, Fifth EditionInserting Characters within a String
  • insert()inserts characters within a string
  • An Introduction to Programming with C++, Fifth EditionSearching 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 EditionSearching a String (continued)An Introduction to Programming with C++, Fifth EditionComparing 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 EditionComparing a Portion of a String with Another String (continued)An Introduction to Programming with C++, Fifth EditionComparing a Portion of a String with Another String (continued)An Introduction to Programming with C++, Fifth EditionDuplicating a Character within a String Variable
  • Use assign()to duplicate a character a specified number of times
  • An Introduction to Programming with C++, Fifth EditionDuplicating a Character within a String Variable (continued)An Introduction to Programming with C++, Fifth EditionConcatenating 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 EditionConcatenating Strings (continued)It is easier to use:hyphens.assign(5, '-');An Introduction to Programming with C++, Fifth EditionSummary
  • String manipulation is a common programming task
  • An Introduction to Programming with C++, Fifth EditionSummary (continued)An Introduction to Programming with C++, Fifth EditionApplication 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
    Recommended
    View more...
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks
    SAVE OUR EARTH

    We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

    More details...

    Sign Now!

    We are very appreciated for your Prompt Action!

    x