add: array exercises

This commit is contained in:
Inanc Gumus
2018-12-05 15:02:03 +03:00
parent 25c15716b6
commit 91e17cbeb3
27 changed files with 690 additions and 113 deletions

View File

@ -0,0 +1,31 @@
package main
// ---------------------------------------------------------
// EXERCISE: Assign the Arrays
//
// 1. Create an array named books
// 2. Add book titles to the array
// 3. Create two more copies of the array named: upper and lower
// 4. Change the book titles to uppercase in the upper array only
// 5. Change the book titles to lowercase in the lower array only
// 6. Print all the arrays
// 7. Observe that the arrays are not connected when they're copied.
//
// NOTE
// Check out the strings package, it has functions to convert letters to
// upper and lower cases.
//
// BONUS
// Invent your own arrays with different types other than string,
// and do some manipulations on them.
//
// EXPECTED OUTPUT
// Note: Don't worry about the book titles here, you can use any title.
//
// books: ["Kafka's Revenge" "Stay Golden" "Everythingship"]
// upper: ["KAFKA'S REVENGE" "STAY GOLDEN" "EVERYTHINGSHIP"]
// lower: ["kafka's revenge" "stay golden" "everythingship"]
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"strings"
)
func main() {
books := [...]string{"Kafka's Revenge", "Stay Golden", "Everythingship"}
upper, lower := books, books
for i := range books {
upper[i] = strings.ToUpper(upper[i])
lower[i] = strings.ToLower(lower[i])
}
fmt.Printf("books: %q\n", books)
fmt.Printf("upper: %q\n", upper)
fmt.Printf("lower: %q\n", lower)
}