-
Notifications
You must be signed in to change notification settings - Fork 1
/
expandstr2.go
37 lines (32 loc) · 1022 Bytes
/
expandstr2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import "os"
// import "fmt"
func main() {
args := os.Args[1:]
if len(args) != 1 {
return
}
strSlice := splitt(args[0], " ")
result := strSlice[0]
for i := 1; i < len(strSlice); i++ {
result = result + " " + strSlice[i]
}
os.Stdout.WriteString(result + "\n")
}
func splitt(s, sp string) []string {
strs := []string{}
index := 0
for i := 0; i+len(sp) <= len(s); i++ {
if s[i:i+len(sp)] == sp {
if s[index:i] != "" {
strs = append(strs, s[index:i])
}
index = i + len(sp)
} else if i+len(sp) == len(s) { // if len s[i:i+len(sp)]!= sep and i+len(sp)=len(s) (WHIICH MEANS we are at the last iteration) WHIICH MEANS if len s[len(s)-len(sep):len(s))]!= sep WHIICH MEANS if the end of the string doesn't end with a sep
strs = append(strs, s[index:]) // in this case append all to the end
}
}
// fmt.Printf("%#v\n", strs) // this line allow us to see clearly how and why we added the condition "if s[index:i]!=""{" above
return strs
}
// checkpoint / using split function