1 / 47

Manipulating Characters and Strings

11. Manipulating Characters and Strings. An Introduction to Programming with C++ 2nd edition By Diane Zak. 11. Objectives. After completing this overview, you will be able to: Control the case of characters and strings Determine the number of characters contained in a string

winda
Download Presentation

Manipulating Characters and Strings

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. 11 Manipulating Characters and Strings An Introduction to Programming with C++ 2nd edition By Diane Zak

  2. 11 Objectives • After completing this overview, you will be able to: • Control the case of characters and strings • Determine the number of characters contained in a string • Compare a portion of a string variable’s contents to another string • Determine whether a string is contained within a string variable

  3. 11 Objectives • After completing this overview, you will be able to: • Replace a portion of a string variable’s contents with another string • Assign a portion of a string variable’s contents to another string variable • Duplicate a character within a string variable • Concatenate strings • Clear the contents of the DOS window

  4. 11 Character and String Manipulation • Many times, a program will need to manipulate (process) character or string data • Most programming languages provide built-in functions that make character and string manipulation an easy task • Most of the functions discussed in this lesson have more than one syntax • However, because the purpose of this tutorial is simply to introduce you to character and string manipulation, you typically will be shown only one syntax for each function

  5. 11 Controlling the Case of a Character • Character comparisons involving letters of the alphabet are case-sensitive • Rather than using a logical operator when comparing characters that represent letters of the alphabet, you also can use a function that temporarily converts one or both of the letters to either uppercase or lowercase before the comparison is made

  6. 11 Syntax and Examples of the C++ toupper and tolower Functions

  7. 11 Controlling the Case of a Character • As Figure 11-2 shows, the toupper and tolower functions have one actual argument, charVariable • The charVariable argument is the name of a char variable, and is passed by value to both functions • The toupper function converts the value it receives to uppercase, and then returns the result

  8. 11 Controlling the Case of a Character • The tolower function, on the other hand, returns the result converting the value it receives to lowercase • Neither function changes the value stored in the charVariable argument • You also can use the toupper and tolower functions to assign the uppercase and lowercase versions of a character to a variable

  9. 11 Controlling the Case of a String • Like character comparisons, string comparisons are case-sensitive, which means that the string “Yes” is not the same as the string “YES” or the string “yes” • Before using a string in a comparison, you can convert it to either uppercase or lowercase, and then use the converted string in the comparison

  10. 11 Syntax and Examples of the C++ transform Function

  11. 11 Syntax and Examples of the C++ transform Function

  12. 11 Controlling the Case of a String • String in the syntax of the C++ transform function is the name of a string variable that contains the string you want converted, or transformed, to either uppercase or lowercase • The transform function converts each of the characters contained in the range specified in the function’s first two arguments—beginning with the character whose location is specified in the first argument

  13. 11 Illustration of state.begin() and state.end()

  14. 11 Determining the Number of Characters Contained in a String • In many programs, it is necessary to determine the number of characters contained in a string

  15. 11 Syntax and Examples of the C++ size Function

  16. 11 Comparing a Portion of a string Variable’s Contents to Another String • You can use the comparison operators (>, >=, <, <=, ==, and !=) to compare two strings • In some programs, rather than comparing two entire strings, you may need to compare a portion of one string to another string • In the compare function’s syntax, string1 and string2 are the two strings you want to compare • String1 is the name of string variable, and string2 can be a string literal constant or the name of a string variable

  17. 11 Syntax and Examples of the C++ compare Function

  18. 11 Determining Whether a String Is Contained Within a string Variable • In some programs, you may need to determine whether a specific string is contained with a string variable • In the find function’s syntax, string is the name of a string variable whose contents you want to search, and subString is the string for which you are searching • The find function searches for the subString in the string, starting with the character in position startFind in the string

  19. 11 Syntax and Examples of the C++ find Function

  20. 11 Syntax and Examples of the C++ find Function

  21. 11 Replacing a Portion of a string Variable’s Contents • When you use the assignment operator (=) to assign a string to a string variable, the string replaces all of the characters previously stored in the variable • In situations where you need to replace only a portion of a string variable’s contents, rather than the entire contents, you need to use a function

  22. 11 Replacing a Portion of a string Variable’s Contents • In the replace function’s syntax, string is the name of a string variable that contains one or more characters you want to replace • The first argument in the replace function, startReplace, specifies where (i.e. in what character position) to begin replacing characters in the string • The second argument, numberOfCharsToReplace, indicates the number of characters to replace

  23. 11 Syntax and Examples of the C++ replace Function

  24. 11 Assigning a Portion of One string Variable to Another string Variable • You can use the assignment operator (=) to assign the entire contents of one string variable to another string variable • Most programming languages provide a built-in function that you can use to assign a portion of one string variable’s contents to another string variable

  25. 11 Syntax and Examples of the C++ assign Function

  26. 11 Assigning a Portion of One string Variable to Another string Variable • In the assign function’s syntax, destinationString and sourceString are the names of string variables, and startAssign and numberOfCharsToAssign are either numeric literal constants or the names of numeric variables • StartAssign indicates the location of the first character in the sourceString to assign to the destinationString, and numberOfCharsToAssign indicates the number of characters to assign from the sourceString • The C++ assign function also has a syntax that can be used to duplicate one character a specified number of times, and then assign the resulting string to a string variable

  27. 11 Another Version of the assign Function’s Syntax, and Examples of Using the Function to Duplicate a Character

  28. 11 Concatenating Strings • Connecting (or linking) strings together is called concatenating • Most programming languages provide a special operator, called the concatenation operator, that you use to concatenate strings

  29. 11 Examples of Using the C++ Concatenation Operator

  30. 11 Summary • Most programming languages provide built-in functions for processing character and string data—a common task performed by many programs • The C++ language, for example, provides the toupper and tolower functions for converting characters to uppercase and lowercase, respectively • You can use the C++ size function to determine the number of characters contained in a string, and the C++ compare function to compare a portion of a string variable’s contents to another string

  31. 11 Summary • You can use the C++ replace function to replace a portion of a string variable’s contents with another string • Connecting (or linking) strings together is called concatenating • The concatenation operator in C++ is the plus sign (+)

  32. 11 Analyzing, Planning, and Desk-Checking the Hangman Algorithm • The IPO chart shows that the output is the guessed word, and the input is the original word (provided by player 1) and one or more letters (provided by player 2) • The Output column indicates that the guessed word will contain five dashes when the program begins; each dash represents a letter in the original word • The IPO chart shows that the program will use two processing items: position and number of dashes replaced

  33. 11 IPO Chart for One Possible Solution to the Hangman Game Problem

  34. 11 Analyzing, Planning, and Desk-Checking the Hangman Algorithm • The position item will be used to store the character position of player 2’s letter within the original word • The first step in the algorithm shown in Figure 11-19 is to get the original word from player 1 • Next, the program will use an outer loop that repeats its instructions while the number of dashes replaced (in the guessed word) is less than five

  35. 11 Analyzing, Planning, and Desk-Checking the Hangman Algorithm • The next instruction in the outer loop is the beginning of a nested loop that repeats its instructions while the value in the position item is greater than or equal to zero

  36. 11 Coding the Hangman Game Algorithm • The program will require five variables to store the values of its input, processing, and output items • You will use the string data type for the three variables that store the input and output items • You will name the variables origWord, letter, and guessWord

  37. 11 Current Status of the Program’s Code

  38. 11 Clearing the Contents of the DOS Windows • You can use the C++ system function to clear the contents of the DOS window • The syntax of the system function is system(command), where command is an operating system instruction enclosed in quotation marks • After clearing the contents of the DOS window, the program should display the five dashes contained in the guessed word

  39. 11 Clearing the Contents of the DOS Windows • You can use the statements cout<<“Guess this word: “<< guessWord << endl; for this purpose • The next step in the algorithm is the beginning of the outer loop, which repeats its instructions while the number of dashes replaced in the guessed word is less than five • The next step in the algorithm is the beginning of the nested loop, which repeats its instructions while the value in the position variable is greater than or equal to zero

  40. 11 Current Status of the Program’s Code

  41. 11 Completed Code for the Hangman Game Program

  42. 11 Completed Code for the Hangman Game Program

  43. 11 Data and Completed Desk-Check Table for the Program

  44. 11 Completing the Hangman Program • To open the partially completed C++ program, then complete the program and test it, refer to the procedures shown on pages 412-415 of the textbook

  45. 11 Completed Hangman Game Program

  46. 11 Completing the Hangman Program

  47. 11 DOS Window Showing Result of First Test

More Related