Skip to content

Instantly share code, notes, and snippets.

@tejzpr
Created April 1, 2021 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejzpr/67385f6243b2da340d99defcfbee7c6c to your computer and use it in GitHub Desktop.
Save tejzpr/67385f6243b2da340d99defcfbee7c6c to your computer and use it in GitHub Desktop.
strstr in go
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strstr("Hello how how are you", "How"))
}
func strstr(haystack string, needle string) []int {
if len(haystack) <= 0 {
return []int{0}
}
var positions = make([]int, 0)
var (
i int
j int
index int
)
var needleLength = len(needle)
for i = 0; i < len(haystack); i++ {
index = i
j = 0
for strings.ToLower(string(haystack[index])) == strings.ToLower(string(needle[j])) {
if j == needleLength-1 {
positions = append(positions, i)
j = 0
}
index++
j++
}
}
return positions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment