Skip to content

Scrabble

Scrabble tiles spelling HELLO and WORLD

Problem to Solve

In Scrabble, players form words to earn points. A word's score is the sum of the point values of all its letters.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 3 3 2 1 4 2 4 1 8 5 1 3 1 1 3 10 1 1 1 1 4 4 8 4 10

For example, CODE is worth 7 points: C is worth 3, O is worth 1, D is worth 2, and E is worth 1.

Create a file named scrabble.c inside a folder named scrabble. Write a C program that determines the winner of a short Scrabble-like game. Ask Player 1 and Player 2 to enter one word each, calculate both scores, and print one of these results:

Player 1 wins!
Player 2 wins!
Tie!

Uppercase and lowercase letters have the same point value. Characters that are not letters are worth 0 points.

Advice and Hints

Begin with a small program that compiles, and then solve the problem one step at a time. The overall algorithm has three parts:

  1. Read one word from each player.
  2. Compute the score of each word.
  3. Compare the scores and print the result.

The local version of this task uses standard C. Text is stored in arrays of char

Start with a compilable program

Create scrabble.c with the required headers, a scoring-function prototype, and the main structure of the program:

#include <ctype.h>
#include <stdio.h>

int compute_score(const char word[]);

int main(void)
{
    char word1[100];
    char word2[100];

    // Prompt both players for words.

    // Compute both scores.

    // Print the winner.
}

int compute_score(const char word[])
{
    // Compute and return the score of word.
}
Read the two words

A 100-element character array has room for 99 input characters and the final null character, \0. Read each word with a width limit:

printf("Player 1: ");
scanf("%99s", word1);

printf("Player 2: ");
scanf("%99s", word2);

You may assume that each player enters one word of no more than 99 characters.

Store the letter values

An array can associate each letter with its point value. Index 0 represents A, index 1 represents B, and so on:

const int LETTER_POINTS[26] = {
    1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3,
    1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10
};

Place this array outside the functions if both main and compute_score need to access it.

Compute a word's score

Visit each character until you reach \0. If the character is uppercase, subtract 'A' to obtain an index from 0 to 25. If it is lowercase, subtract 'a' instead. Use that index to look up the point value.

isupper and islower from ctype.h can distinguish the two cases. Characters that satisfy neither test add no points.

Convert a character to unsigned char before passing it to a ctype.h function, for example:

isupper((unsigned char) word[i])
Print the winner

Call compute_score once for each word. Then use if, else if, and else to distinguish the three possible outcomes:

  • the first score is greater;
  • the second score is greater; or
  • the scores are equal.

How to Test

Compile and run the program from the scrabble folder:

cc scrabble.c -o scrabble
./scrabble

If your system provides gcc instead of cc, run gcc scrabble.c -o scrabble.

Your program should behave like these examples:

$ ./scrabble
Player 1: Question?
Player 2: Question!
Tie!
$ ./scrabble
Player 1: red
Player 2: wheelbarrow
Player 2 wins!
$ ./scrabble
Player 1: COMPUTER
Player 2: science
Player 1 wins!
$ ./scrabble
Player 1: Scrabble
Player 2: wiNNeR
Player 1 wins!

Continue with Caesar.