IC210 – Lab 8

Strings. . . Arrays of Characters in Disguise

# Your flowchart/pseudocode for the each function will likely be short, but remember that you are to focus on the conceptually difficult parts of the program’s/function’s control flow logic.  Reminder: All pre-lab homework is due at the beginning of the lab period, and late pre-lab homework will earn a grade of 0.

1. 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

1. 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`

1. 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

1. 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.

1. 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.

1. 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.

GOING BEYOND

a.)    Modify your game so that instead of asking for the puzzle word, it randomly selects a word from this list of words.

b.)    Modify your game so that you start with 7 points, then lose a point whenever you guess a letter that does not exist in the puzzle word. If you run out of points, you lose.

c.)    Modify your game so that it can correctly handle a “puzzle word” that contains multiple words (e.g. a string that contains spaces).  How will you handle reading in such puzzle words from a file?