[Codewars] Split Strings

Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_').

Examples:

Solution("abc") //should return ["ab", "c_"]
Solution("abcdef") //should return ["ab", "cd", "ef"]

先判斷 mod 是不是 == 1 是的話就先在最後面加上 _

然後 split 成 array

loop array 把 i 和 i + 1 的值丟進去 result

Golang

package kata

import (
  "fmt"
  "strings"
       )

func Solution(str string) []string {
  var r []string
  if(len(str) % 2) == 1 {
    // should append "_"
    	str = fmt.Sprintf("%v%v", str, "_")
  }

  a := strings.Split(str, "")
  
  for i := 0; i < len(a); i++ {
    t := fmt.Sprintf("%v%v", a[i], a[i+1])
    r = append(r, t)
    i++
  }
  return r
}