Drill

ProblemsC# › games

Score a word by letter positions

easygamesStringsMathC#

A word game scores each letter by its position in the alphabet: a is 1, b is 2, and so on up to z is 26.

WordScore(word: string) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int WordScore(string word) {
    
}

Worked examples

CallResult
WordScore("abc")6
WordScore("hello")52
WordScore("")0
WordScore("a")1

Hint

Subtract the character code of a from each letter to get its value.

Reference solution in C#
public int WordScore(string word) {
    int sum = 0;
    for (int i = 0; i < word.Length; i++) {
        char c = word[i];
        if (c >= 'a' && c <= 'z') sum += c - 'a' + 1;
    }
    return sum;
}

The same problem in another language

More games problems in C#