Subslices and chemical elements

Report a typo

In the periodic table, the chemical elements are abbreviated with the first one or two letters of their name, for example:

  • He is Helium

  • B is Boron

  • Ne is Neon

  • Ca is Calcium

  • I is Iodine

Below you will see a Go program with the elements slice. Your task is to use slice expressions to create a substring to form the abbreviation for every element and then print each substring on a single line.

Write a program in Go
package main

import "fmt"

func main() {
elements := []string{"Helium", "Boron", "Neon", "Calcium", "Iodine"}

elem1 := elements[0][?] // abbreviation of Helium
elem2 := elements[1][?] // abbreviation of Boron
elem3 := elements[2][?] // abbreviation of Neon
elem4 := elements[3][?] // abbreviation of Calcium
elem5 := elements[4][?] // abbreviation of Iodine

// DO NOT delete the line below; it prints the substrings!
fmt.Println(elem1, elem2, elem3, elem4, elem5)
}
___

Create a free account to access the full topic