The code which I posted below is a part of my whole program. The rest of the code is not related to this question. Is there any solution other than using this long switch statement? The main objective is to shorten the code. The if statement is used simply to capitalize lowercase letters.
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)
{
// TODO: Compute and return a score for string
int word_length = strlen(word);
int i, sum = 0;
for (i = 0; i < word_length; i++)
{
//This if statement is used to captalise lower-case letters.
if (islower(word[i]))
{
word[i] = toupper(word[i]);
}
//This switch statement is the key in this program. It will check
//each letter of word and then modify variable sum according to it.
switch (word[i])
{
case 'A':
sum = sum + POINTS[0];
break;
case 'B':
sum = sum + POINTS[1];
break;
case 'C':
sum = sum + POINTS[2];
break;
case 'D':
sum = sum + POINTS[3];
break;
case 'E':
sum = sum + POINTS[4];
break;
case 'F':
sum = sum + POINTS[5];
break;
case 'G':
sum = sum + POINTS[6];
break;
case 'H':
sum = sum + POINTS[7];
break;
case 'I':
sum = sum + POINTS[8];
break;
case 'J':
sum = sum + POINTS[9];
break;
case 'K':
sum = sum + POINTS[10];
break;
case 'L':
sum = sum + POINTS[11];
break;
case 'M':
sum = sum + POINTS[12];
break;
case 'N':
sum = sum + POINTS[13];
break;
case 'O':
sum = sum + POINTS[14];
break;
case 'P':
sum = sum + POINTS[15];
break;
case 'Q':
sum = sum + POINTS[16];
break;
case 'R':
sum = sum + POINTS[17];
break;
case 'S':
sum = sum + POINTS[18];
break;
case 'T':
sum = sum + POINTS[19];
break;
case 'U':
sum = sum + POINTS[20];
break;
case 'V':
sum = sum + POINTS[21];
break;
case 'W':
sum = sum + POINTS[22];
break;
case 'X':
sum = sum + POINTS[23];
break;
case 'Y':
sum = sum + POINTS[24];
break;
case 'Z':
sum = sum + POINTS[25];
break;
}
}
return sum;
}
