scrabble.c

Harvard CS50 - Problem Set 2

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

int POINTS[] = {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};
int compute_score(string word);

int main(void)
{
    // prompt for words
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // compute score
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // print winner
    if (score1 > score2)
    {
    printf("Player 1 wins: \n");
    }
    else if (score1 < score2)
    {
    printf("Player 2 wins: \n");
    }
    else
    {
    printf("Tie! \n");
    }
    return 0;
}

int compute_score(string word)
{
    // Keep track of score
    int score = 0;

    // Compute score for each character
    for (int i = 0, len = strlen(word); i < len; i++)
    {
        if (isupper(word[i]))
        {
            score += POINTS[word[i] - 'A'];
        }
        else if (islower(word[i]))
        {
            score += POINTS[word[i] - 'a'];
        }
    }

    return score;
}

Last updated

Was this helpful?