1 / 81

An Introduction to Programming with C++ Sixth Edition

An Introduction to Programming with C++ Sixth Edition. Chapter 13 Strings. Objectives. Utilize string memory locations in a program Get string input using the getline function Ignore characters using the ignore function Determine the number of characters in a string

cherit
Télécharger la présentation

An Introduction to Programming with C++ Sixth 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++Sixth Edition Chapter 13 Strings

  2. Objectives • Utilize string memory locations in a program • Get string input using the getline function • Ignore characters using the ignore function • Determine the number of characters in a string • Access the characters in a string • Search a string An Introduction to Programming with C++, Sixth Edition

  3. Objectives (cont’d.) • Remove characters from a string • Replace characters in a string • Insert characters within a string • Duplicate characters within a string • Concatenate strings An Introduction to Programming with C++, Sixth Edition

  4. The string Data Type • The string data type is not a fundamental data type in C++ • Added to C++ through use of string class • To use the string class, a program must contain the #include <string> directive • You can use the string class to create string variables or string named constants • Memory locations of type string are initialized with string literal constants—0 or more characters enclosed in double quotation marks (“”) An Introduction to Programming with C++, Sixth Edition

  5. The string Data Type (cont’d.) Figure 13-1 How to declare and initialize string variables and named constants An Introduction to Programming with C++, Sixth Edition

  6. The Creative Sales Program • Program gets a salesperson’s name and sales amount from keyboard • Calculates salesperson’s bonus and displays salesperson’s name and bonus amount • Extraction operator can be used to get string input from keyboard • Stops reading characters when it encounters a white-space character (blank, tab, or newline character) An Introduction to Programming with C++, Sixth Edition

  7. The Creative Sales Program (cont’d.) Figure 13-2 Problem specification and IPO chart for the Creative Sales program An Introduction to Programming with C++, Sixth Edition

  8. The Creative Sales Program (cont’d.) Figure 13-3 How to use the extraction operator (>>) to get string input An Introduction to Programming with C++, Sixth Edition

  9. The getline Function • The getline function obtains string data from the keyboard and stores it in a string variable • Syntax is: getline(cin, stringVariableName [, delimiterCharacter]); • Three actual arguments (first two required): • cin argument refers to computer keyboard • stringVariableName argument is name of a string variable in which to store input • Optional delimiterCharacter indicates character that immediately follows last character of string An Introduction to Programming with C++, Sixth Edition

  10. The getline Function (cont’d.) • Function will continue to read characters entered at keyboard until it encounters a delimiter character • Default delimiter character is newline character • When the function encounters a delimiter character, it discards the character—process called consuming the character • Newline character is designated by ‘\n’ • Backslash is called an escape character • Backslash and character following it are called an escape sequence An Introduction to Programming with C++, Sixth Edition

  11. Figure 13-4 How to use the getline function to get string input from the keyboard An Introduction to Programming with C++, Sixth Edition

  12. Figure 13-5 Creative Sales program An Introduction to Programming with C++, Sixth Edition

  13. The getline Function (cont’d.) Figure 13-6 Sample run of the Creative Sales program An Introduction to Programming with C++, Sixth Edition

  14. Figure 13-7 Partial Creative Sales program showing the modifications Figure 13-8 Results of running the modified Creative Sales program An Introduction to Programming with C++, Sixth Edition

  15. The ignore Function • The ignore function instructs the computer to read and ignore characters stored in the cin object by consuming (discarding) them • Syntax is: • cin.ignore([numberOfCharacters][,delimiterCharacter]); • Has two actual arguments, both optional: • numberOfCharacters argument is maximum number of characters function should consume (default is 1) • delimiterCharacter argument stops ignore function from reading and discarding any more characters when consumed An Introduction to Programming with C++, Sixth Edition

  16. Figure 13-9 How to use the ignore function An Introduction to Programming with C++, Sixth Edition

  17. Figure 13-10 Modified Creative Sales program with the ignore function An Introduction to Programming with C++, Sixth Edition

  18. The ignore Function (cont’d.) Figure 13-11 Sample run of the modified Creative Sales program with the ignore function An Introduction to Programming with C++, Sixth Edition

  19. The ZIP Code Program • Program reads in a string from the user and verifies that it contains exactly five characters • If string contains exactly five characters, program displays “Valid length” • Otherwise, displays “Invalid length” An Introduction to Programming with C++, Sixth Edition

  20. The ZIP Code Program (cont’d.) Figure 13-12 Problem specification and IPO chart for the ZIP Code program An Introduction to Programming with C++, Sixth Edition

  21. Determining the Number of Characters Contained in a string Variable • You use string class’s length function to determine the number of characters in a string variable • Syntax is: • string.length() • Returns number of characters contained in string An Introduction to Programming with C++, Sixth Edition

  22. Determining the Number of Characters Contained in a string Variable (cont’d.) Figure 13-13 How to use the length function An Introduction to Programming with C++, Sixth Edition

  23. Figure 13-13 How to use the length function (cont’d.) An Introduction to Programming with C++, Sixth Edition

  24. Figure 13-14 The ZIP code program An Introduction to Programming with C++, Sixth Edition

  25. Determining the Number of Characters Contained in a string Variable (cont’d.) Figure 13-15 Sample run of the ZIP code program An Introduction to Programming with C++, Sixth Edition

  26. Accessing the Characters Contained in a string Variable • The substr function allows you to access any number of characters contained in a string variable by returning the specified characters • Syntax is: • string.substr(subscript[, count]) • Has two arguments (first is required): • subscript argument represents subscript of first character you want to access in string • count argument is number of characters to return after character specified by subscript An Introduction to Programming with C++, Sixth Edition

  27. Accessing the Characters Contained in a string Variable (cont’d.) • If you omit count argument, function returns all characters from subscript position through end of string • A string is equivalent to a one-dimensional array of characters • Each character has a unique subscript that indicates character’s position in the string An Introduction to Programming with C++, Sixth Edition

  28. Accessing the Characters Contained in a string Variable (cont’d.) Figure 13-16 How to use the substr function An Introduction to Programming with C++, Sixth Edition

  29. Figure 13-16 How to use the substr function (cont’d.) An Introduction to Programming with C++, Sixth Edition

  30. Accessing the Characters Contained in a string Variable (cont’d.) Figure 13-17 Modified problem specification for the ZIP code program An Introduction to Programming with C++, Sixth Edition

  31. Figure 13-17 Modified IPO chart information and C++ instructions for the ZIP code program (cont’d.) An Introduction to Programming with C++, Sixth Edition

  32. Figure 13-17 Modified IPO chart information and C++ instructions for the ZIP code program (cont’d.) An Introduction to Programming with C++, Sixth Edition

  33. Accessing the Characters Contained in a string Variable (cont’d.) Figure 13-17 Modified IPO chart information and C++ instructions for the ZIP code program (cont’d.) An Introduction to Programming with C++, Sixth Edition

  34. Accessing the Characters Contained in a string Variable (cont’d.) Figure 13-18 Modified ZIP code program An Introduction to Programming with C++, Sixth Edition

  35. Figure 13-18 Modified ZIP code program (cont’d.) An Introduction to Programming with C++, Sixth Edition

  36. Accessing the Characters Contained in a string Variable (cont’d.) Figure 13-19 Sample run of the modified ZIP code program An Introduction to Programming with C++, Sixth Edition

  37. The Rearranged Name Program • Program allows user to enter a person’s first and last names, separated by a space • Displays the person’s last name followed by a comma, a space, and the person’s first name • Searches the input string for the space that separates the first and last names An Introduction to Programming with C++, Sixth Edition

  38. Figure 13-20 Problem specification and IPO chart for the rearranged name program An Introduction to Programming with C++, Sixth Edition

  39. Searching the Contents of a string Variable • You use the find function to search contents of a string variable to determine whether it contains a specific sequence of characters • Syntax is: • string.find(searchString, subscript) • searchString argument is astring for which you are searching within string • subscript argument specifies starting position for the search An Introduction to Programming with C++, Sixth Edition

  40. Searching the Contents of a string Variable (cont’d.) • find function performs a case-sensitive search (uppercase and lowercase letters are not equivalent) • When searchString is contained within string, function returns an integer that indicates beginning position of searchString within string • Function returns -1 when searchString is not contained within string An Introduction to Programming with C++, Sixth Edition

  41. Searching the Contents of a string Variable (cont’d.) Figure 13-21 How to use the find function An Introduction to Programming with C++, Sixth Edition

  42. Figure 13-21 How to use the find function (cont’d.) An Introduction to Programming with C++, Sixth Edition

  43. Figure 13-22 Rearranged name program An Introduction to Programming with C++, Sixth Edition

  44. Searching the Contents of a string Variable (cont’d.) Figure 13-23 Sample run of the rearranged name program An Introduction to Programming with C++, Sixth Edition

  45. The Annual Income Program • Program allows the user to enter a company’s annual income • Removes any commas and spaces from user’s entry before displaying annual income An Introduction to Programming with C++, Sixth Edition

  46. The Annual Income Program (cont’d.) Figure 13-24 Problem specification for the annual income program An Introduction to Programming with C++, Sixth Edition

  47. Figure 13-24 IPO chart for the annual income program (cont’d.) An Introduction to Programming with C++, Sixth Edition

  48. Removing Characters from a string Variable • You can use the erase function to remove one or more characters from a string variable • Syntax is: • string.erase(subscript[, count]); • subscript is position of first character you want to remove • Optional count argument is an integer that specifies number of characters you want removed • If you omit count, function removes all characters from subscript through end of string An Introduction to Programming with C++, Sixth Edition

  49. Removing Characters from a string Variable (cont’d.) Figure 13-25 How to use the erase function An Introduction to Programming with C++, Sixth Edition

  50. Figure 13-25 How to use the erase function (cont’d.) An Introduction to Programming with C++, Sixth Edition

More Related