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

39
Section13/1.go Normal file
View File

@@ -0,0 +1,39 @@
/*
Coding Exercise #1
1. Using the var keyword declare a string called name and initialize it with your name.
2. Using short declaration syntax declare a string called country and assign the country you are living in to the string variable.
3. Print the following string on multiple lines like this:
Your name: `here the value of name variable`
Country: `here the value of country variable`
4. Print out the following strings:
a) He says: "Hello"
b) C:\Users\a.txt
*/
package main
import "fmt"
func main() {
var name string = "Andrei"
country := "Romania"
fmt.Printf("Your name: %s\nCountry: %s\n", name, country)
//equivalent to:
fmt.Printf(`Your name: %s
Country: %s
`, name, country)
fmt.Println("He says: \"Hello\"")
fmt.Println("C:\\Users\\a.txt")
}

29
Section13/2.go Normal file
View File

@@ -0,0 +1,29 @@
/*
1. Declare a rune called r that stores the non-ascii letter ă
2. Print out the type of r
3. Declare 2 strings that contains the values ma and m
4. Concatenate the strings and the rune in a new string (the new string will hold the value mamă ).
BTW: mamă means mother in Romanian.
Note: You should convert rune to string to contatenate it to another string.
*/
package main
import "fmt"
func main() {
r := 'ă' // declaring a rune
fmt.Printf("r type:%T\n", r) // rune is alias to int32
s1, s2 := "ma", "m" // declaring 2 strings
// concatenating strings
s := s1 + s2 + string(r) // converting rune to string (expliction conversion is required)
fmt.Printf("s is %s\n", s) // => s is mamă
}

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()
}

21
Section13/4.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s1 := "Go is cool!"
x := s1[0]
fmt.Println(x)
// ERROR -> cannot assign to s1[0]
// s1[0] = 'x'
// printing the number of runes in the string
// fmt.Println(len(s1))
fmt.Println(utf8.RuneCountInString(s1))
}

31
Section13/5.go Normal file
View File

@@ -0,0 +1,31 @@
/*
Consider the following string declaration:s := "你好 Go!"
1. Convert the string to a byte slice.
2. Print out the byte slice
3. Iterate over the byte slice and print out each index and byte in the byte slice
*/
package main
import "fmt"
func main() {
s := "你好 Go!"
// converting string to byte slice
b := []byte(s)
// printing out the byte slice
fmt.Printf("%#v\n", b)
// iterating over the byte slice and printing out each index and byte in the byte slice
for i, v := range b {
fmt.Printf("%d -> %d\n", i, v)
}
}

31
Section13/6.go Normal file
View File

@@ -0,0 +1,31 @@
/*
Consider the following string declaration:s := "你好 Go!"
1. Convert the string to a rune slice.
2. Print out the rune slice
3. Iterate over the rune slice and print out each index and rune in the rune slice
*/
package main
import "fmt"
func main() {
s := "你好 Go!"
// converting string to rune slice
r := []rune(s)
// printing out the rune slice
fmt.Printf("%#v\n", r)
// iterating over the rune slice and printing out each index and rune in the rune slice
for i, v := range r {
fmt.Printf("%d -> %c\n", i, v)
}
}