Created
April 1, 2021 22:07
-
-
Save tejzpr/67385f6243b2da340d99defcfbee7c6c to your computer and use it in GitHub Desktop.
strstr in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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