0

I'm not familiar with C, not sure what this error means.. could anyone help?

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Points assigned to each letter of the alphabet
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};
const char *alphabet[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);

    // TODO: Print the winner
    printf("score1, %d\n", score1);
}

int compute_score(string word)
{
    string myString = word;
    int word_length = strlen(myString); // 4
    int array_length = sizeof(alphabet)/sizeof(alphabet[0]); // 26
    int COUNT = 0;
    for (int i = 0; i < word_length; i++) { // 4
        for(int j = 0; j < array_length; j++) { // 26
            if(alphabet[j] == myString[i])
            {
                COUNT+=1;
            }
        }
    }
    return COUNT;
}

1 Answer 1

0

Each element of the alphabet array is a char * ( a char pointer aka a string). One possible solution would be to change the alphabet array to a char array (and all the double-quotes changed to single quotes in the declaration). Another solution: change the conditional comparison here
if(alphabet[j] == myString[i]) to compare the first char in the string alphabet[j] (ie index 0).

There are also solutions that would eliminate the need for the alphabet array, remembering that one can find the alphabetic index (eg a is 0, b is 1, etc) by "reducing" the ascii value of a char (eg
someLetter - 'a', assuming someLetter is lower case).

You must log in to answer this question.

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