Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.

Similar presentations


Presentation on theme: "Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition."— Presentation transcript:

1 Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition

2 Introduction to Programming with C++, Fourth 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

3 Introduction to Programming with C++, Fourth 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

4 Introduction to Programming with C++, Fourth Edition 4 Manipulating Strings Programs often need to manipulate (process) string data –Verify that an inventory part number begins with a specific letter –Determine whether the last three characters in an employee number are valid

5 Introduction to Programming with C++, Fourth Edition 5 Determining the Number of Characters Contained in a String length() function: determines the number of characters contained in a string Returns an unsigned integer; you need to use the int type cast when assigning the return value to an int variable string.length()

6 Introduction to Programming with C++, Fourth Edition 6 Syntax and Examples of the length() Function

7 Introduction to Programming with C++, Fourth Edition 7 Syntax and Examples of the length() Function (continued)

8 Introduction to Programming with C++, Fourth Edition 8 Removing Characters from a String An application may need to remove characters from an item of data entered by the user –Remove a dollar sign from the beginning of a sales amount –Remove a percent sign from the end of a tax rate Use the erase() function to remove one or more characters located anywhere in a string variable string.erase(subscript[, count])

9 Introduction to Programming with C++, Fourth Edition 9 Removing Characters from a String (continued) Each character in a string is assigned a unique number (subscript) that indicates the character’s position in the string (starting from 0) subscript is the subscript of the first character you want removed from the string count is the number of characters you want removed If you omit count, erase() removes all characters from subscript to the end of the string

10 Introduction to Programming with C++, Fourth Edition 10 Syntax and Examples of the erase() Function

11 Introduction to Programming with C++, Fourth Edition 11 Accessing Characters Contained in a String An application may need to access one or more characters contained in a string –Determine whether the letter “K” appears as the third character in a string –Display only the string’s first five characters Use the substr() function to access any number of characters contained in a string variable string.substr(subscript[, count])

12 Introduction to Programming with C++, Fourth Edition 12 Accessing Characters Contained in a String (continued) substr() function contains two arguments: –Subscript is the subscript of the first character you want to access in the string (starting from 0) –The count argument, which is optional, specifies the number of characters you want to access Returns a string with count number of characters, beginning with the character whose subscript is specified by subscript If you omit count, it returns all the characters from the subscript position through the end of the string

13 Introduction to Programming with C++, Fourth Edition 13 Syntax and Examples of the substr() Function

14 Introduction to Programming with C++, Fourth Edition 14 Replacing Characters in a String Use the replace() function to replace a sequence of characters in a string variable with another sequence of characters –Use the replace() function to replace area code “800” with area code “877” in a phone number string.replace(subscript, count, replacementString)

15 Introduction to Programming with C++, Fourth Edition 15 Replacing Characters in a String (continued) string is the name of a string variable that contains one or more characters you want to replace subscript specifies where to begin replacing characters in the string count indicates the number of characters to replace replacementString contains the string that will replace the characters in the string

16 Introduction to Programming with C++, Fourth Edition 16 Syntax and Examples of the Replace() Function

17 Introduction to Programming with C++, Fourth Edition 17 Inserting Characters within a String Use the insert() function to insert characters within a string variable –Insert an employee’s middle initial within his or her name –Insert parentheses around the area code in a phone number string.insert(subscript, insertString)

18 Introduction to Programming with C++, Fourth Edition 18 Inserting Characters within a String (continued) subscript specifies where in the string you want the insertString inserted (0 is the beginning) Returns a string with the appropriate characters inserted

19 Introduction to Programming with C++, Fourth Edition 19 Syntax and Examples of the insert() Function

20 Introduction to Programming with C++, Fourth Edition 20 Searching a String Use the find() function to search a string variable to determine whether it contains a specific sequence of characters –Determine whether the area code “312” appears in a phone number –Determine whether the street name “Elm Street” appears in an address string.find(searchString, subscript)

21 Introduction to Programming with C++, Fourth Edition 21 Searching a String (continued) string: the string variable you want to search searchString: the string you are searching for subscript: the starting position for the search Search for searchString in string, starting with the character in position subscript in the string If searchString is found in the string, return the beginning position of searchString in the string Return –1 if the searchString is not found

22 Introduction to Programming with C++, Fourth Edition 22 Syntax and Examples of the find() Function

23 Introduction to Programming with C++, Fourth Edition 23 Syntax and Examples of the find() Function (continued)

24 Introduction to Programming with C++, Fourth Edition 24 Comparing a Portion of a string Variable’s Contents to Another String You can use comparison operators with strings Sometimes you need to compare a portion of one string –Compare the last two characters in the employNum variable to “12” to determine if the employee works in the Accounting department, which has a department code of 12 Use the compare() function to compare a portion of a string variable’s contents to another string string1.compare(subscript, count, string2)

25 Introduction to Programming with C++, Fourth Edition 25 Comparing a Portion of a string Variable’s Contents to Another String (continued) string1 and string2 are strings to compare string1 is a string variable and string2 can be a string literal constant or a string variable subscript specifies where in string1 the comparison should begin count indicates the number of characters in string1 to compare to the characters in string2

26 Introduction to Programming with C++, Fourth Edition 26 Syntax and Examples of the compare() Function

27 Introduction to Programming with C++, Fourth Edition 27 Syntax and Examples of the compare() Function (continued)

28 Introduction to Programming with C++, Fourth Edition 28 Duplicating a Character within a string Variable Use the assign() function to duplicate one character a specified number of times, then assign the resulting string to a string variable string.assign(count, character)

29 Introduction to Programming with C++, Fourth Edition 29 Duplicating a Character within a string Variable (continued) string is the name of a string variable that will store the duplicated characters count is either a numeric literal constant or the name of a numeric variable –Indicates the number of times you want to duplicate the character specified in the function’s character argument character can be either a character literal constant or the name of a char variable

30 Introduction to Programming with C++, Fourth Edition 30 Syntax and Examples of the assign() Function

31 Introduction to Programming with C++, Fourth Edition 31 Concatenating Strings Connecting (or linking) strings together is called concatenating Use the concatenation operator (+) to concatenate strings

32 Introduction to Programming with C++, Fourth Edition 32 Examples of Using the Concatenation Operator

33 Introduction to Programming with C++, Fourth Edition 33 Summary Programs often need to manipulate (process) string data length() determines the number of characters contained in a string variable erase() removes characters in a string substr() accesses characters in a string replace() replaces a sequence of characters in a string

34 Introduction to Programming with C++, Fourth Edition 34 Summary (continued) insert() inserts characters in a string find() searches a string to determine whether it contains a specific sequence of characters compare() compares a portion of a string variable’s contents to another string assign() duplicates one character a specified number of times, then assigns the resulting string to a string variable Use + to concatenate (join) strings


Download ppt "Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition."

Similar presentations


Ads by Google