Home > Blockchain >  Academic project - Scrabble - issue trying to convert strong argument into an array of characters
Academic project - Scrabble - issue trying to convert strong argument into an array of characters

Time:12-12

I am working on creating a game of scrabble. I have created a function to iterate through each letter and add it the points for each player. I keep running into an error when as I am trying to convert the word argument (which will be a string) into an array of characters.

Here is the error I am getting (I am using the CS50 IDE)

scrabble.c:44:27: error: incompatible pointer to integer conversion assigning to 'char' from 'string' (aka 'char *'); dereference with * [-Werror,-Wint-conversion]
    wordarray[wordlength] = word;
                          ^ ~~~~
                            *
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Points assigned to each letter of the alphabet
int POINTS[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};
char LETTERS[26] = {'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'};

int compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // Print the winner
    if (score1 > score2) {

        printf("Player 1 is the winner!");

    } else if (score1 < score2) {

        printf("Player 2 is the winner!");

    } else {

        printf("It is a tie!");

    }
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
**    //Turn word into an array of characters
    int wordlength = strlen(word);
    char wordarray[wordlength]; 
    wordarray[wordlength] = word;**
    //Placeholder for players total score
    int playerscore = 0;
    //Placeholder for index value
    int index;
    //Placeholder for word value
    int wordvalue;
    
    //Loop throught the word array 
    for (int i = 0; wordarray[i] < strlen(word); i  ) {

         //the current letter being iterated through. Convert it to lowercarse
         char letter = tolower(wordarray[i]);

        //iterate through all the letters in the alphabet
        for (int j = 0; j < 25; j  ) {

        //if the letter is in the alphabet 
            if (letter == LETTERS[j]) {
        //assign its index value to index
                index = LETTERS[j];
            }
        }
        //match letter with value of points, assign to wordvalue
        wordvalue = POINTS[index];
        //push the wordvalue to the total player score 
        playerscore  = wordvalue;

}      
        //iterate through all letters and return the playerscore 
        return playerscore;

}

CodePudding user response:

char wordarray[wordlength]; is the declaration of an array of char, with a length of wordlength.

The valid indices for accessing this array are [0, wordlength - 1].

The following

wordarray[wordlength] = word;

accesses the array at the invalid index of worldlength, and attempts to place the value of word at that position in the array. word is a string (actually an alias of char *), not a char, so the compiler warns you about this.

This is not how you copy a string to an array. For that, use strcpy. Do note that you will need one extra char for the null terminating byte ('\0') required to make a string.

char wordarray[wordlength   1];
strcpy(wordarray, word);

That said, creating a copy of the string is not at all a requirement for this task. Just loop through the contents of the string given, find the position of each element of word in the LETTERS array, and sum the values found at the same indices in POINTS.

You mostly had this, just overthought it a bit.

int compute_score(string word)
{
    int score = 0;

    for (size_t i = 0; word[i]; i  )
    {
        for (size_t j = 0; j < 26; j  )
        {                      
            if (tolower((unsigned char) word[i]) == LETTERS[j])   
            {                           
                score  = POINTS[j];     
                break;                  
            }                                                               
        }
    }

    return score;
}
  • Related