This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
swb6-it-sec/Lab04/code/part01/main.go
2022-05-30 10:07:48 +02:00

33 lines
930 B
Go

package main
import (
"fmt"
"github.com/dariubs/percent"
"strings"
)
var alphabet = []rune{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
}
func letterCount(inputText string) {
count := make([]int, len(alphabet))
frequency := make([]float64, len(alphabet))
totalCount := 0
for index, letter := range alphabet {
count[index] = strings.Count(inputText, string(letter))
count[index] += strings.Count(inputText, string(letter+32))
totalCount += count[index]
}
for index := range alphabet {
frequency[index] = percent.PercentOf(count[index], totalCount)
}
for index, letter := range alphabet {
fmt.Printf("The letter %c (%c) occurs %d times in the text and the frequency in percent is %0.2f\n", letter, letter+32, count[index], frequency[index])
}
}
func main() {
letterCount("This is a test to check for something.Zz")
}