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};
string word = get_string("Player 1: ");
int score[26];
int sum=0;
int i=0;
while (word[i]!='\0')//string to alphanum
{
    if (isupper(word[i]))
    {
        word[i]=(int)word[i];
        score[i]=word[i]-65;

    }
    else if (islower(word[i]))
    {
        word[i]=(int)word[i];
        score[i]=word[i]-97;

    }



    printf("%d\t",score[i]);

    i++;
}
printf("\n");
for (int j=0;score[j]!='\0';j++)
{
    score[j]=POINTS[score[j]];
    printf("%d \t",score[j]);
}

the code works for all the values apart from A\a, for some reason it stops working , ive first converted the input charater array(word[]) into interger array,then tried to link the score array ( represents alphabets position==> A-0,B-1,C-2,..,Z-25) to the POINTS array which represents the scrabble value for each letter (Z-10,A-1)

enter image description here

2

There are 2 best solutions below

1
Lundin On

One problem is that doing arithmetic on the symbol table values like 'A' + 1 == 'B' etc is actually not well-defined or guaranteed to work, save for the symbols '0' to '9' where such a guarantee is made by the C standard.

This means that you can't have a look-up score table of 26 scores and calculate the index in that table based on character value subtracted by 'A' (or 65). If CS-50 is saying otherwise, it is bad.

Luckily however, mainstream computers have lots of memory these days so we may create a look-up table like this instead:

const unsigned char letter_score [256] =
{
  ['A'] = 1, ['a'] = 1,
  ['B'] = 3, ['b'] = 3,
  ...
};

This is taking up 256 bytes instead of 26 but that's no big deal. We set up each index of the table through so called "designated intializers", the ['A'] = things - this is an easy way to create self-documenting code. Items left out of the array initialization are guaranteed to get set to zero, which is fine in this case.

As you noticed, this also eliminated the isupper/toupper etc run-time checks, if we just set 'A' and 'a' to the same value. That's an optimization for faster execution speed, at the cost of ROM memory.

Usage:

size_t length = strlen(word);
int score = 0;
for(size_t i=0; i<length; i++)
{
  score += letter_score[ word[i] ];
}

As you can see, there's no need for score to be an array, since we don't really need to know/preserve the information that letter number n gave x points. We just want to know the total score.

1
whyking On

thanks for all the replies , i got it to work ,things which u guyz suggested and i changed was removing score array and calculate the sum on the go ,that fixed up the code and got rid of all the confusion i had and it also compacted the code quite a bit

int compute_score(string word)
{
    int sum=0;
    int i=0;
    while (word[i]!='\0')//string to alphanum
    {
        if (isupper (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-65];
        }
        else if (islower (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-97];
        }
        i++;
    }
    return sum;
}

This is the final code

and if someone wants the complete code , spoilers for someone trying to solve cs50 on thier own

#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};

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
    if (score1>score2)
    {
        printf("Player 1 Wins! by %d points\n",score1);
    }
    else if (score1<score2)
    {
        printf("Player 2 Wins! by %d points\n",score2);
    }
    else
    {
        printf("Tie!\n");
    }
}

int compute_score(string word)
{
    int sum=0;
    int i=0;
    while (word[i]!='\0')//string to alphanum
    {
        if (isupper (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-65];
        }
        else if (islower (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-97];
        }
        i++;
    }
    return sum;
}