0

This is my code:

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

int compute_score(string word)
{
   
    int i = 0;
    int m = word[i]; 
    // TODO: Compute and return score for string
    for(int n = strlen(word) ; i < n ; i++)
    {
     
        if( word[i] <= 90 && word[i] >= 65)
        {   
        m = m - 64;
        }
        else if( word[i] <= 122 && word[i] >= 97)
        {   
        m = m - 96;
        }
        else
        {
        m = 0;
        }
    printf("%i",m);
    printf("%i \n",POINTS[ m-1]);
    }
   
    return 0;
}

I did the bottom 2 printf functions test it as I go. There are no errors it can detect, but for the printf("%i \n",POINTS[ m-1]); function I am getting: (A:1) (Z:8) (a:1) (z:10) for those values.

Does anyone know what went wrong?

1 Answer 1

0

When does word[1] (or any letter after thefirst) get evaluated? In the for loop m is a function of m. Once m is set to word[0] outside the function, no other letter in the word is considered. Does the first printf display the expected value? It will be used as the index to POINTS, which should be in the range 0 - 25.

You must log in to answer this question.

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