exercices of section 13
This commit is contained in:
35
Section13/3.go
Normal file
35
Section13/3.go
Normal 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()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user