0

Hi, anyone can help me. I can't get the result I need for this code. I need to compare words whether player 1 or 2 wins. I only get result for tie. I dont know which line to fix. I new in learning programming so I hope anyone can help me here. Thanks!

#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");

    if (score1 > score2)
    {
        printf("Player 1 wins!\n");
    }
    else if (score2 > score1)
    {
        printf("Player 2 wins!\n");
    }
        else {
            printf("Tie!");
        }

    }

    int compute_score(string word)
    
{
    // TODO: Compute and return score for string
    int sum_points = 0;
    for (int i = 0; i < strlen(word); i++)
    {
        if (isalpha(word[i]))
        {
              int letter_offset = toupper(word[i]) - 'a';
              sum_points += POINTS[letter_offset];
        }
    }
    return sum_points;
}
0

There is a mistake here letter_offset = toupper(word[i]) - 'a';. toupper and a are "mismatched".

You must log in to answer this question.

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