SPEAKER 1: Let's take a look at how you might have solved the Scrabble problem.
Recall that in this problem your task was
to write a program in C that could take two different words
and figure out which one of them would have been worth
more points in a game of Scrabble.
Let's see how you might have done that.
We gave you some distribution code in scrabble.c already.
But the place you probably wanted to start
was by writing this compute score function.
This function that accepted a word as input
and could tell you how many points that word was worth.
So let's scroll down to the compute score function.
Ultimately, in order to figure out how many points the word is worth,
we want to go through each of the characters one by one
and figure out how much each character is worth,
adding up all of those results.
As we add up all of those results, it's going
to be helpful to have some variable that's keeping track of the score
so far.
And so that's the first thing that we do inside this compute score function.
We have an integer called score equal to zero.
And that variable is going to keep track of the score for the current word.
Now what we need to do is loop over the entire word,
looking at each character one at a time.
To do so, we have a for loop, where we start with int i equals 0.
i is going to represent the index of the character.
And in order to complete this loop, we need to know how long the string is.
So to do that, we can use the strlen function
to access the length of the string, storing that inside of a variable
called len.
We're going to repeat this loop as long as i is less than len,
increasing i by one every time this loop completes an iteration.
Ultimately, this is going to have i start at zero,
the first character in the string, then become 1, 2, 3,
and so forth all the way until we get to the end of the string.
Now there are two cases we need to consider here.
This character might be an uppercase letter, in which case
it's worth points, or it might be a lowercase letter, in which case
it's also going to be worth some number of points.
And for any other type of character, like a digit, or a punctuation,
or a space, those are not going to be worth any points at all.
So let's first deal with the uppercase situation.
We can use the isupper function to check if any particular character is
an uppercase letter.
And so we can check for word square bracket i, meaning,
take that string "word" and access character
i for whatever i happens to be for this iteration of the loop.
If this current character, word bracket i is an uppercase letter,
well then we need to update the score.
How will we know how to update the score?
Well ultimately, we're going to use that points array at the top of our file.
This array gives us the point value for each letter
of the alphabet, where points square brackets 0 represents
the score for the letter A. Points square bracket 1 represents
the score for the letter B, and so on.
But if you recall from our ASCII relationship between characters
and what numbers represent those characters,
capital letter A is not actually represented by the number 0.
It's represented by the number 65.
And the capital letter B is represented by the number 66.
And capital C by 67.
So we need to somehow translate those ASCII values 65, 66, 67 for A, B, and C
into 0, 1, and 2, which will be the indexes we
want to use into this points array.
How do we do that?
Well, ultimately, every letter's ASCII value for the uppercase letters
is 65 more than whatever index value we actually want
to use inside of this points array.
So in order to translate that character into its corresponding index,
I could say, words square bracket i minus 65,
where capital A, which is 65 minus 65 would give us 0.
So ultimately, we're accessing POINTS square brackets
0 to get access to the number of points that corresponds to capital letter A.
For capital letter B, it would have been 66 minus 65.
And that would give us POINTS square bracket
1 for accessing the number of points we would get for a capital B.
But since 65 is really the same thing as capital A in terms of how they're
represented, it's a little bit cleaner in my code
to just say word i minus capital A. That way
I don't need to know that 65 just so happens
to correspond to the capital letter A.
So ultimately what I'm doing here is saying word
square bracket i minus capital A. That is going to give me an index value.
If word square bracket i is capital A, then it's capital A minus capital A.
That's zero.
If word square bracket i is capital Z, then I'm
going to get capital Z minus capital A, which is going to be 25.
Those values correspond to an index in the points array.
So by putting that in square brackets inside of POINTS,
I can access how many points that particular character
is going to be worth.
Whatever number of points that character is worth,
I'm going to then add that to the score.
And that's going to update the value of score in response to this character.
Then I need to do the same thing for lower case letters.
If islower, this particular character, we're going to do the same logic,
adding to the score as well.
But this time, subtracting lowercase A, 97, instead of uppercase A, 65.
To make sure that we're able to get the correct index inside of that points
array.
This condition has an if and an else if.
We don't need an else condition.
The else case would handle characters that are not letters at all.
Numbers or punctuation, for example.
But in those cases, since we don't need to do anything,
the score is not going to change, we can just leave that case off entirely.
We only consider uppercase and lowercase letters.
And at the very end, once we have our final value for score,
we can return the value of score.
What is our main function that I need to do with that information?
Well, we need to print the winner.
So we can compare those scores.
If score1 is greater than score2, we print "Player 1 wins!"
Otherwise, if score1 is less than score2, we print "Player 2 wins!"
And else, the only other possibility is that both players
have the same score, in which case our program is going
to print that there has been a tie.
Once you compile and run that program, you should be able to type in two words
and see which one of them would win in a game of Scrabble My name is Brian.
And this was Scrabble.