# Why does it only find eight (and not nine) differences?

I’m working on a small script that compares two strings. I’ve tested the following with various strings and it returns the correct value yet in this case it says there are only eight differences when there are nine.

``````myName := "GGACGGATTCTG"
yourName := "AGGACGGATTCT"
var count int
l := len(myName)

for z := 0; z < (l - 1); z++ {
x := string((myName)[z])
y := string((yourName)[z])

if x != y {
count = count + 1
}
}
fmt.Println(count)
``````

Hi. Change for loop to range from 0 while z < l and not l - 1.

2 Likes

And you don’t have to convert the bytes to string to compare them.

2 Likes

As Johan wrote, try something like this:

``````package main

import "fmt"

func main() {
var count int

myName := "GGACGGATTCTG"
yourName := "AGGACGGATTCT"

for z := 0; z < len(myName); z++ {
if myName[z] != yourName[z] {
count = count + 1
}
}
fmt.Println(count)
}
``````

At the Go Playground: https://play.golang.org/p/6In2-_WGI8x

1 Like

Thank you both so much!

I ended up with this as I still don’t know how to use `range` properly

``````func main() {

var (
a     = "GGACGGATTCTG"
b     = "AGGACGGATTCT"
count int
)

if len(a) != len(b) {
os.Exit(1)
}

fmt.Println("The string is", len(a), "characters long")
for z := 0; z < (len(a)); z++ {
x := a[z]
y := b[z]

if x != y {
count = count + 1
}
}

if count == 0 {
fmt.Println("There were no differences")
} else if count == 1 {
fmt.Println("There is 1 character that is different")
} else {
fmt.Println("There are", count, "characters that are different")
}
}``````
1 Like