Untitled
unknown
c_cpp
3 years ago
1.7 kB
14
Indexable
#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: "); int i = 0; for (int v = 0; v < strlen(word1); v++) { while (word1[i] > 64 && word1[i] < 91 ) { word1[i] = tolower(word1[i]); } i++; } int w = 0; for (int d = 0; d < strlen(word2); d++) { while (word2[w] > 64 && word2[w] < 91 ) { word2[w] = tolower(word2[w]); } w++; } // Score both words int score1 = compute_score(word1); int score2 = compute_score(word2); printf("%i\n", score1); printf("%i\n", score2); // 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 l = 0; int c = 0; for (int i = 0; i < strlen(word); i++) { int j = 97; if (word[l] < 97 || word[l] > 122) { word[l] = 0; } else { for (int x = 0; x < 26; x++) { if (word[l] == j) { word[l] = POINTS[j - 97]; l++; } j++; } } } l = c + 1; c++; int sum = 0; for (int k = 0; k < strlen(word); k++) { sum += word[k]; } return sum; }
Editor is loading...