From eba1e3ec494d6cc7a0d0a66d5d135292cad2280a Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 5 Feb 2025 12:17:08 +0100 Subject: [PATCH] exercices of section 13 --- Section13/1.go | 39 +++++++++++++++++++++++++++++++++++++++ Section13/2.go | 29 +++++++++++++++++++++++++++++ Section13/3.go | 35 +++++++++++++++++++++++++++++++++++ Section13/4.go | 21 +++++++++++++++++++++ Section13/5.go | 31 +++++++++++++++++++++++++++++++ Section13/6.go | 31 +++++++++++++++++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 Section13/1.go create mode 100644 Section13/2.go create mode 100644 Section13/3.go create mode 100644 Section13/4.go create mode 100644 Section13/5.go create mode 100644 Section13/6.go diff --git a/Section13/1.go b/Section13/1.go new file mode 100644 index 0000000..61f5393 --- /dev/null +++ b/Section13/1.go @@ -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") + +} diff --git a/Section13/2.go b/Section13/2.go new file mode 100644 index 0000000..e62000e --- /dev/null +++ b/Section13/2.go @@ -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ă + +} diff --git a/Section13/3.go b/Section13/3.go new file mode 100644 index 0000000..44e48ec --- /dev/null +++ b/Section13/3.go @@ -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() + +} diff --git a/Section13/4.go b/Section13/4.go new file mode 100644 index 0000000..aa19296 --- /dev/null +++ b/Section13/4.go @@ -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)) + +} diff --git a/Section13/5.go b/Section13/5.go new file mode 100644 index 0000000..73007d7 --- /dev/null +++ b/Section13/5.go @@ -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) + } + +} diff --git a/Section13/6.go b/Section13/6.go new file mode 100644 index 0000000..b1c8fcd --- /dev/null +++ b/Section13/6.go @@ -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) + } + +}