#include <cs50.h>
#include <stdio.h>
#include <ctype.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!\n");
}
else if (score2 > score1)
{
printf("Player 2 wins!\n");
}
else if (score1 == score2)
{
printf("Tie!\n");
}
}
int compute_score(string word)
{
// TODO: Compute and return score for string
int sum = 0;
for (int i = 0, n = strlen(word); i < n; i++)
{
if (isupper(word[i]))
{
word[i - 65] = POINTS[i];
sum = sum + POINTS[i];
}
//if slower do:
else if (islower(word[i]))
{
word[i - 97] = POINTS[i];
sum = sum + POINTS[i];
}
else
{
sum = sum + 0;
}
}
return sum;
}
-
You should edit your question to provide us with what errors you are encountering, and adding what you have already tried is a good habit to get into, too. This saves us some time by knowing what error to start looking for.– Robert S. PrattAug 11, 2021 at 16:40
Add a comment
|