Manipulating Characters and Strings - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Manipulating Characters and Strings

Description:

... will contain five dashes when the program begins; each dash represents a letter ... program will use two processing items: position and number of dashes replaced ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 48
Provided by: informat701
Category:

less

Transcript and Presenter's Notes

Title: Manipulating Characters and Strings


1
Manipulating Characters and Strings
11
  • An Introduction to Programming with C
  • 2nd edition
  • By Diane Zak

2
Objectives
11
  • 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 variables contents
    to another string
  • Determine whether a string is contained within a
    string variable

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

4
Character and String Manipulation
11
  • 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
Controlling the Case of a Character
11
  • 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
Syntax and Examples of the C toupper and
tolower Functions
11
7
Controlling the Case of a Character
11
  • 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
Controlling the Case of a Character
11
  • 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
Controlling the Case of a String
11
  • 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
Syntax and Examples of the C transform
Function
11
11
Syntax and Examples of the C transform
Function
11
12
Controlling the Case of a String
11
  • 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 functions first two argumentsbeginning with
    the character whose location is specified in the
    first argument

13
Illustration of state.begin() and state.end()
11
14
Determining the Number of Characters Contained
in a String
11
  • In many programs, it is necessary to determine
    the number of characters contained in a string

15
Syntax and Examples of the C size Function
11
16
Comparing a Portion of a string Variables
Contents to Another String
11
  • You can use the comparison operators (gt, gt, lt,
    lt, , 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 functions 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
Syntax and Examples of the C compare Function
11
18
Determining Whether a String Is Contained Within
a string Variable
11
  • In some programs, you may need to determine
    whether a specific string is contained with a
    string variable
  • In the find functions 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
Syntax and Examples of the C find Function
11
20
Syntax and Examples of the C find Function
11
21
Replacing a Portion of a string Variables
Contents
11
  • 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 variables contents, rather
    than the entire contents, you need to use a
    function

22
Replacing a Portion of a string Variables
Contents
11
  • In the replace functions 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
Syntax and Examples of the C replace Function
11
24
Assigning a Portion of One string Variable to
Another string Variable
11
  • 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 variables contents to another string
    variable

25
Syntax and Examples of the C assign Function
11
26
Assigning a Portion of One string Variable to
Another string Variable
11
  • In the assign functions 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
Another Version of the assign Functions Syntax,
and Examples of Using the Function to Duplicate
a Character
11
28
Concatenating Strings
11
  • 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
Examples of Using the C Concatenation Operator
11
30
Summary
11
  • Most programming languages provide built-in
    functions for processing character and string
    dataa 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 variables contents to another string

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

32
Analyzing, Planning, and Desk-Checking the
Hangman Algorithm
11
  • 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
IPO Chart for One Possible Solution to the
Hangman Game Problem
11
34
Analyzing, Planning, and Desk-Checking the
Hangman Algorithm
11
  • The position item will be used to store the
    character position of player 2s 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
Analyzing, Planning, and Desk-Checking the
Hangman Algorithm
11
  • 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
Coding the Hangman Game Algorithm
11
  • 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
Current Status of the Programs Code
11
38
Clearing the Contents of the DOS Windows
11
  • 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
Clearing the Contents of the DOS Windows
11
  • You can use the statements coutltltGuess this
    word ltlt guessWord ltlt 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
Current Status of the Programs Code
11
41
Completed Code for the Hangman Game Program
11
42
Completed Code for the Hangman Game Program
11
43
Data and Completed Desk-Check Table for the
Program
11
44
Completing the Hangman Program
11
  • 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
Completed Hangman Game Program
11
46
Completing the Hangman Program
11
47
DOS Window Showing Result of First Test
11
Write a Comment
User Comments (0)
About PowerShow.com