Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.

Similar presentations


Presentation on theme: "An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings."— 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 Edition2

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 Edition3

4 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 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 ( “” )

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

6 An Introduction to Programming with C++, Sixth Edition6 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)

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

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

9 An Introduction to Programming with C++, Sixth Edition9 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

10 An Introduction to Programming with C++, Sixth Edition10 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

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

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

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

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

15 An Introduction to Programming with C++, Sixth Edition15 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

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

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

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

19 An Introduction to Programming with C++, Sixth Edition19 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”

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

21 An Introduction to Programming with C++, Sixth Edition21 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

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

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

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

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

26 An Introduction to Programming with C++, Sixth Edition26 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

27 An Introduction to Programming with C++, Sixth Edition27 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

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

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

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

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

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

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

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

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

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

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 Edition37

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

39 An Introduction to Programming with C++, Sixth Edition39 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

40 An Introduction to Programming with C++, Sixth Edition40 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

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

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

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

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

45 An Introduction to Programming with C++, Sixth Edition45 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

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

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

48 An Introduction to Programming with C++, Sixth Edition48 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

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

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

51 An Introduction to Programming with C++, Sixth Edition51 Figure 13-26 Annual income program Removing Characters from a string Variable (cont’d.)

52 An Introduction to Programming with C++, Sixth Edition52 Figure 13-26 Annual income program (cont’d.)

53 An Introduction to Programming with C++, Sixth Edition53 Figure 13-27 Sample run of the annual income program Removing Characters from a string Variable (cont’d.)

54 An Introduction to Programming with C++, Sixth Edition54 Replacing Characters in a string Variable The replace function replaces one sequence of characters in a string variable with another Syntax is: –string.replace(subscript, count, replacementString); –subscript argument specifies where to begin replacing characters in string –count argument indicates number of characters to replace –replacementString argument contains replacement characters

55 An Introduction to Programming with C++, Sixth Edition55 Replacing Characters in a string Variable Figure 13-28 How to use the replace function

56 An Introduction to Programming with C++, Sixth Edition56 Figure 13-28 How to use the replace function (cont’d.)

57 An Introduction to Programming with C++, Sixth Edition57 Replacing Characters in a string Variable (cont’d.) Figure 13-29 Partial annual income program showing the replace function

58 An Introduction to Programming with C++, Sixth Edition58 The Social Security Number Program Program allows user to enter a Social Security number without hyphens If user’s entry contains nine characters, program inserts hyphens in appropriate places and displays number on screen If user does not enter nine characters, program displays an appropriate message

59 An Introduction to Programming with C++, Sixth Edition59 Figure 13-30 Problem specification and IPO chart for the Social Security number program

60 An Introduction to Programming with C++, Sixth Edition60 Inserting Characters within a string Variable You can use the insert function to insert characters into a string variable Syntax is: –string.insert(subscript, insertString); –subscript specifies where in string you want characters inserted –insertString specifies characters to be inserted

61 An Introduction to Programming with C++, Sixth Edition61 Figure 13-31 How to use the insert function

62 An Introduction to Programming with C++, Sixth Edition62 Figure 13-32 Social Security number program

63 An Introduction to Programming with C++, Sixth Edition63 Figure 13-33 Sample run of the Social Security number program Inserting Characters within a string Variable

64 An Introduction to Programming with C++, Sixth Edition64 The Company Name Program Program allows user to enter name of a company Displays name with a row of hyphens below it

65 An Introduction to Programming with C++, Sixth Edition65 The Company Name Program (cont’d.) Figure 13-34 Problem specification and IPO chart for the company name program

66 An Introduction to Programming with C++, Sixth Edition66 Duplicating a Character within a string Variable You can use the assign function to duplicate one character a specified number of times and assign the resulting string to a string variable Syntax is: –string.assign(count, character); –count argument is an integer that indicates the number of times you want to duplicate the character specified in character argument –character argument can be either a character literal constant or a char memory location

67 An Introduction to Programming with C++, Sixth Edition67 Figure 13-35 How to use the assign function

68 An Introduction to Programming with C++, Sixth Edition68 Figure 13-36 Company name program

69 An Introduction to Programming with C++, Sixth Edition69 Duplicating a Character within a string Variable (cont’d.) Figure 13-37 Sample run of the company name program

70 An Introduction to Programming with C++, Sixth Edition70 Concatenating Strings String concatenation refers to the process of connecting (linking) strings together You concatenate strings using the concatenation operator ( + sign)

71 An Introduction to Programming with C++, Sixth Edition71 Figure 13-38 How to use the concatenation operator

72 An Introduction to Programming with C++, Sixth Edition72 Concatenating Strings (cont’d.) Figure 13-39 Partial company name program showing string concatenation

73 Summary The string data type was added to the C++ language using the string class Memory locations whose data type is string are initialized using string literal constants (0 or more characters enclosed in double quotation marks) You can use the extraction operator to get a string from the user at the keyboard if the string does not contain a white-space character An Introduction to Programming with C++, Sixth Edition73

74 Summary (cont’d.) The getline function gets a string of characters entered at the keyboard and stores them in a string variable The string can contain any characters, including white-space characters The getline function reads and stores characters from the keyboard until it encounters a delimiter character, which it consumes (discards) The default delimiter character is the newline character An Introduction to Programming with C++, Sixth Edition74

75 Summary (cont’d.) The computer stores characters entered at the keyboard in the cin object Both the extraction operator and the getline function remove characters from cin The extraction operator leaves the newline character in cin, while the getline function consumes the newline character The ignore function reads and then consumes characters entered at the keyboard An Introduction to Programming with C++, Sixth Edition75

76 Summary (cont’d.) The ignore function stops reading and consuming characters when it consumes either a specified number of characters or a delimiter character The default number of characters to consume is 1 The assign, erase, insert, and replace functions are self-contained statements that change the value of a string variable The concatenation operator ( + ) is used to join (link) strings together An Introduction to Programming with C++, Sixth Edition76

77 Lab 13-1: Stop and Analyze Study the code in Figure 13-40 and then answer the questions An Introduction to Programming with C++, Sixth Edition77

78 Lab 13-2: Plan and Create An Introduction to Programming with C++, Sixth Edition78 Figure 13-41 Problem specification for Lab 13-2

79 Lab 13-3: Modify Make a copy of Lab 13-2 to modify Current version allows player 1 to enter only five- letter words Modify the program so that player one can enter a word of any length Test the program appropriately An Introduction to Programming with C++, Sixth Edition79

80 Lab 13-4: Desk-Check Desk-check the code in Figure 13-45 What will the code display on the screen? An Introduction to Programming with C++, Sixth Edition80

81 Lab 13-5: Debug Run the program in the Lab13-5.cpp file Type Joe and press Enter Rather than displaying the letters J, o, and e on three separate lines, the program displays Joe, oe, and e Debug the program An Introduction to Programming with C++, Sixth Edition81


Download ppt "An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings."

Similar presentations


Ads by Google