0

I'm currently working on scrabble and I'm unsure how to go about making 'word' in line with POINTS[], so they match and have the same values for letters. This is the code I have so far, only recently figured that to index into word, I use word[i] as it will vary how long this array is, dependent on what the user types in. Thank you!

int compute_score(string word) { // TODO: Compute and return score for string for(int i=0; i <strlen(word), i++)// loop through string {

    if(islower(word[i])==true)// *checks if ascii is between 97-122*
    {
        POINTS[0]*//index into points, use characters in word*
                 *// subtract difference in line with points*
    }            *//return int*
    if(isupper(word[i])==true)*// checks if ascii is between 65-90*
    {
        POINTS[0]


    }
    else
    {
        *//score as 0*
    }
}

}

1 Answer 1

1

You are on the right track. You iterate through each character in the word. word[i] is the ith character. So far so good. Then you have 2 conditions to check whether the current character is lower case or upper case. That is good too. So now you need to figure out how to get the points for this character.

Notice that the POINTS array contains points for each character. And that the array is ordered by alphabet - i.e. points for 'a' is in the very first position (POINTS[0] since we are counting from zero), points for 'b' is in POINTS[1] etc

Now we need to figure out how convert 'a' to 0, 'b' to 1 etc We can do this by subtracting the ASCII code - e.g. If word[i] contains a lower case letter then: word[i] - 97 will give us the desired outcome. E.g. is word[i] contains 'b' then word[i] - 97 = 1 (i.e. 98-97=1)

In C you can even do things like index = word[i] - 'a'; and then you can reference POINTS[index] to get the number of points for this specific character.

Hope that's enough for you to complete the task

3
  • Okay, thank you very much, I think I'm really close now, I just don't understand the final sentence of referencing POINTS[index] to get the number of points for a specific character.
    – Robbie
    Nov 28, 2022 at 11:39
  • POINTS[] array contains the number of points you get for each character. POINTS[0] contains 1 (for letter A), POINTS[1] contains 3 (for letter B) ... POINTS[25] contains 10 for letter Z. So as you go through each character you start adding scores for each letter in the word. It will look something like: score = score + POINTS[index];
    – DmitryK
    Dec 4, 2022 at 22:43
  • Thank you, this has really helped!
    – Robbie
    Dec 6, 2022 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .