1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <cs50.h> #include <ctype.h> #include <stdio.h> #include <string.h>
int compute_score(string word);
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 main(void) { string word1 = get_string("Player 1: "); string word2 = get_string("Player 2: ");
int score1 = compute_score(word1); int score2 = compute_score(word2);
if (score1 > score2) { printf("Player 1 wins!\n"); return 0; } else if (score1 < score2) { printf("Player 2 wins!\n"); return 0; } else { printf("Tie!\n"); return 0; } }
int compute_score(string word) { int score = 0;
for (int i = 0; i < strlen(word); i++) { if (isupper(word[i])) { score += points[word[i] - 'A']; } else if (islower(word[i])) { score += points[word[i] - 'a']; } } return score; }
|