-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (60 loc) · 1.19 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"strings"
"os"
"github.com/alexflint/go-arg"
)
var args struct {
I bool
V bool
C bool
W bool
Pattern string `arg:"positional"`
Input string `arg:"positional"`
}
func IndexOfSubstring(str, subStr string) []int {
ind := []int{}
fmt.Println(subStr)
fmt.Println(str)
fmt.Println(len(subStr))
for i := 0; i < (len(str)-len(subStr)); i++ {
if str[i:i+len(subStr)] == subStr {
ind = append(ind, i)
}
}
return ind
}
func main() {
arg.MustParse(&args)
file:=args.Input
dat, err := os.ReadFile(file)
if err != nil {
panic(err)
}
pattern := args.Pattern
if(args.I){
index := IndexOfSubstring(strings.ToLower(string(dat)), strings.ToLower(pattern))
fmt.Println(index)
fmt.Println("Match found at index number", index)
}
if(args.V){
str:=string(dat)
res:=strings.ReplaceAll(str,pattern,"")
fmt.Println(res)
}
if(args.C){
index := IndexOfSubstring(strings.ToLower(string(dat)), strings.ToLower(pattern))
fmt.Println(len(index))
}
if(args.W){
ind := []int{}
arr:=strings.Split(string(dat), " ")
for i := 0; i < len(arr); i++{
if(arr[i] == pattern){
ind = append(ind,i)
}
}
fmt.Println("Exact word found at index",ind)
}
}