-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrabblescore.c
59 lines (50 loc) · 1.19 KB
/
scrabblescore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Scrabble Score calculator
*
* calculates the scrabble score for a given word
*
* Brenda Anderson, 11 Sep 2016
*/
#include <stdio.h>
// Prototype
int calculate_score(char* p);
int main(int argc, char* argv[])
{
// make sure user entered a word
if (argc != 2)
{
printf("usage: ./scrabblescore word\n");
return 1;
}
int score = calculate_score(argv[1]);
// print the answer
if (score != 0)
printf("%s is worth %i points\n", argv[1], score);
else
printf("invalid entry\n");
return 0;
}
int calculate_score (char* p)
{
int counter = 0;
// score values 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
static const int scores[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 };
// iterate over the word
while (*p)
{
// calculate the score for each letter
if (*p >= 'a' && *p <= 'z')
{
counter += scores[(*p) - 'a'];
}
else if (*p >= 'A' && *p <= 'Z')
{
counter += scores[(*p) - 'A'];
}
else
{
return 0;
}
p++;
}
return counter;
}