exercices of section 13

This commit is contained in:
2025-02-05 12:17:08 +01:00
parent ec3676ddd4
commit eba1e3ec49
6 changed files with 186 additions and 0 deletions

35
Section13/3.go Normal file
View File

@@ -0,0 +1,35 @@
/*
Consider the following string declaration: s1 := "țară means country in Romanian"
1. Iterate over the string and print out byte by byte
2. Iterate over the string and print out rune by rune and the byte index where the rune starts in the string
Are you stuck? Do you want to see the solution for this exercise?
*/
package main
import "fmt"
func main() {
s1 := "țară means country in Romanian"
// iterating over the string and print out byte by byte
fmt.Printf("Bytes in string: ")
for i := 0; i < len(s1); i++ {
fmt.Printf("%v ", s1[i])
}
fmt.Println()
// iterating over the string and print out rune by rune
// and the byte index where the rune starts in the string
for i, r := range s1 {
fmt.Printf("byte index: %d -> rune: %c\n", i, r)
}
fmt.Println()
}