IC210 – Lab 8
Strings. . . Arrays of Characters in Disguise
Pre-lab homework. None. Prepare for quiz covering functions.
-
This
week you have been studying the topic of arrays. As it turns out,
you have really been using arrays of characters all along but they were
called strings.
-
Thinking
of a string as an array of characters, the string Alpha might look like:
string
Alpha = “m835491”;
|
m
|
8
|
3
|
5
|
4
|
9
|
1
|
|
0
|
1
|
2
|
3
|
4
|
5
|
6
|
-
Like
all other arrays, notice that the subscripts for the elements of the array
of characters called
Alpha, start with 0 (zero). The first element
in the array Alpha
is in the 0th index position.
cout << Alpha[0]; // prints an 'm' on screen
cout << Alpha[6]; // prints a '1' on screen
-
A useful
function provided for use with strings is the length
function. This function returns number of characters contained in
the string. The value of this number will be one larger than the
last index position in the array.
cout << Alpha.length(); // 7 is returned by the function length
and prints
// on the screen
This function can be very useful as an upper bound in a for loop
when you
are trying to print out or examine all the characters in a string.
for (int i=0; i<Alpha.length(); i++) // prints out
m835491
cout << Alpha[i];
Notice that we use '<' and NOT '<='
because Alpha[7]
is outside the bounds of the indices of our array
Alpha.
Alpha
only has
characters in index positions 0 .. 6.
-
Your
task for today's lab is to complete this
program which simulates playing the Wheel of Fortune game you may have
seen on television. A word is entered as the hidden puzzle
word. The player of the game then guesses letters one after the
other trying ultimately to guess what the hidden puzzle word is. As
correct letters are guessed their positions in the hidden puzzle word are
revealed. (The Wheel of Fortune game is similar to another game you
may have played as a child called Hangman, but is less violent.) Note
that the program provided above does not compile as some of the function
return statements still need to be completed.
-
When
you have completed the functions as indicated in the program, build and
run your completed program and ensure that it works. Demo your
program for your instructor once you have it working correctly.