Drill

ProblemsGo › games

Score a word by letter positions

easygamesStringsMathGo

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

Go 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

func wordScore(word string) int {
	
}

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 Go
func wordScore(word string) int {
	sum := 0
	for i := 0; i < len(word); i++ {
		c := word[i]
		if c >= 'a' && c <= 'z' {
			sum += int(c - 'a' + 1)
		}
	}
	return sum
}

The same problem in another language

More games problems in Go