ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# vlzware's solution

## to Scrabble Score in the C Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a word, compute the scrabble score for that word.

## Letter Values

You'll need these:

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

## Examples

"cabbage" should be scored as worth 14 points:

• 3 points for C
• 1 point for A, twice
• 3 points for B, twice
• 2 points for G
• 1 point for E

And to total:

• `3 + 2*1 + 2*3 + 2 + 1`
• = `3 + 2 + 6 + 3`
• = `5 + 9`
• = 14

## Extensions

• You can play a double or a triple letter.
• You can play a double or a triple word.

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_scrabble_score.c

``````#include "vendor/unity.h"
#include "../src/scrabble_score.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_lowercase_letter(void)
{
TEST_ASSERT_EQUAL(1, score("a"));
}

void test_uppercase_letter(void)
{
TEST_IGNORE();               // delete this line to run test
TEST_ASSERT_EQUAL(1, score("A"));
}

void test_valuable_letter(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(4, score("f"));
}

void test_short_word(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(2, score("at"));
}

void test_short_valuable_word(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(12, score("zoo"));
}

void test_medium_word(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(6, score("street"));
}

void test_medium_valuable_word(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(22, score("quirky"));
}

void test_long_mixed_case_word(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(41, score("OxyphenButazone"));
}

void test_english_like_word(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(8, score("pinata"));
}

void test_empty_input(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(0, score(""));
}

void test_entire_alphabet_available(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL(87, score("abcdefghijklmnopqrstuvwxyz"));
}

int main(void)
{
UnityBegin("test/test_scrabble_score.c");

RUN_TEST(test_lowercase_letter);
RUN_TEST(test_uppercase_letter);
RUN_TEST(test_valuable_letter);
RUN_TEST(test_short_word);
RUN_TEST(test_short_valuable_word);
RUN_TEST(test_medium_word);
RUN_TEST(test_medium_valuable_word);
RUN_TEST(test_long_mixed_case_word);
RUN_TEST(test_english_like_word);
RUN_TEST(test_empty_input);
RUN_TEST(test_entire_alphabet_available);

UnityEnd();
return 0;
}``````

### src/scrabble_score.c

``````#include "scrabble_score.h"
#include <stddef.h>
#include <ctype.h>

#define ALL 26

int score(const char *input)
{
if ((input == NULL) || (*input == '\0'))
return 0;

const int scrabble[ALL] = {
/*	A  B  C  D  E  F  G  H  I  J  K  L  M  */
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};
/*	N, O  P  Q   R  S  T  U  V  W  X  Y  Z */

int res = 0;
int tmp;
while (*input)
if ((tmp = toupper(*input++) - 'A') >= 0
&& tmp < ALL)
res += scrabble[tmp];

return res;
}``````

### src/scrabble_score.h

``````#ifndef SCRABBLE_SCORE_H
#define SCRABBLE_SCORE_H

int score(const char *input);

#endif``````