#include <stdbool.h>
#include <string.h>
#include <ctype.h>
// ENSURE THAT A WORD CONTAINS ONLY LOWERCASE CHARACTERS
bool valid_word(char word[])
{
int i = 0;
// IF NOT A LOWERCASE CHARACTER, THE FUNCTION RETURNS false
while(word[i] != '\0') {
if( ! islower( word[i] )) { // if not islower ...
return false;
}
i = i+1;
}
// WE'VE REACHED THE END OF THE WORD - IT'S ALL LOWERCASE
return true;
}
// CALCULATE THE SCRABBLE VALUE OF ANY WORD
int calc_value(char word[])
{
// AN ARRAY TO PROVIDE THE VALUE OF EACH LETTER, FROM 'a' TO 'z'
int values[] = { 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 total = 0;
int i = 0;
// TRAVERSE THE WORD DETERMINING THE VALUE OF EACH LETTER
while(word[i] != '\0') {
total = total + values[ word[i] - 'a' ];
i = i+1;
}
return total;
}
|