Skip to content
Snippets Groups Projects
Commit da4aece4 authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

Solution to exercise of unit 02-06

parent db278dd6
No related branches found
No related tags found
No related merge requests found
module gitlab.reutlingen-university.de/go-exercises/go-starter/exercise_0206
go 1.20
package main
import (
"fmt"
"os"
"unicode/utf8"
)
func printSlice(s []string) {
fmt.Printf("%p –len: %d cap: %d %#v\n", s, len(s), cap(s), s)
}
func main() {
if len(os.Args) > 1 {
printSlice(os.Args)
numberOfRunes := 0
numberOfBytes := 0
for _, word := range os.Args[1:] {
numberOfBytes += len(word)
numberOfRunes += utf8.RuneCountInString(word)
}
fmt.Printf("Number of bytes: %d\n", numberOfBytes)
fmt.Printf("Number of runes: %d\n", numberOfRunes)
fmt.Printf("Average number of bytes per rune: %f\n", float32(numberOfBytes)/float32(numberOfRunes))
} else {
fmt.Printf("Usage: %s <sentence>\n", os.Args[0])
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment