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)
}
