Title: Manipulating Characters and Strings
1Manipulating Characters and Strings
11
- An Introduction to Programming with C
- 2nd edition
- By Diane Zak
2Objectives
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
3Objectives
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
4Character 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
5Controlling 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
6Syntax and Examples of the C toupper and
tolower Functions
11
7Controlling 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
8Controlling 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
9Controlling 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
10Syntax and Examples of the C transform
Function
11
11Syntax and Examples of the C transform
Function
11
12Controlling 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
13Illustration of state.begin() and state.end()
11
14Determining 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
15Syntax and Examples of the C size Function
11
16Comparing 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
17Syntax and Examples of the C compare Function
11
18Determining 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
19Syntax and Examples of the C find Function
11
20Syntax and Examples of the C find Function
11
21Replacing 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
22Replacing 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
23Syntax and Examples of the C replace Function
11
24Assigning 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
25Syntax and Examples of the C assign Function
11
26Assigning 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
27Another Version of the assign Functions Syntax,
and Examples of Using the Function to Duplicate
a Character
11
28Concatenating 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
29Examples of Using the C Concatenation Operator
11
30Summary
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
31Summary
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 ()
32Analyzing, 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
33IPO Chart for One Possible Solution to the
Hangman Game Problem
11
34Analyzing, 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
35Analyzing, 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
36Coding 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
37Current Status of the Programs Code
11
38Clearing 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
39Clearing 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
40Current Status of the Programs Code
11
41Completed Code for the Hangman Game Program
11
42Completed Code for the Hangman Game Program
11
43Data and Completed Desk-Check Table for the
Program
11
44Completing 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
45Completed Hangman Game Program
11
46Completing the Hangman Program
11
47DOS Window Showing Result of First Test
11