This commit is contained in:
a.pivkin 2025-12-28 12:50:20 +03:00
parent 1f098de743
commit c14a385a8a
2 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module main
go 1.25.5

View File

@ -0,0 +1,50 @@
package main
import (
"flag"
"fmt"
)
func findSub(s, sub, repl string) (finStr string) {
if len(s) < len(sub) {
return s[:]
}
found := false
currCount := 0
newStr := ""
if s[:len(sub)] == sub {
newStr = repl
currCount++
found = true
// return
} else {
newStr = string(s[0])
}
var resStr string
if found {
resStr = findSub(s[len(sub):], sub, repl)
} else {
resStr = findSub(s[1:],sub,repl)
}
return newStr + resStr
}
func expand(s,sub,repl string, f func(init string,sub string,repl string) string) string{
res := f(s,sub,repl)
return res
}
func main() {
str := flag.String("str", "lolkeklolkeklol", "Initital string")
sub := flag.String("sub", "kek", "substring to find")
repl := flag.String("repl", "puk", "String to replace with")
// res := findSub(*str, *sub, *repl)
res := expand(*str,*sub,*repl,findSub)
fmt.Println(res)
}