Lenght of a string

Hi there, I was wondering how to find the length of a string in terms of bytes, this is my main code:

package main

import “fmt”

func main() {

const N = 10

parole := make([]string, N)

for i := 0; i < N; i++ {

    fmt.Scan(&parole[i])

}

fmt.Println(piuCorta(parole))

}

and I have to write → func piuCorta(parole []string) int { ← , so I’ve written this func:

func piuCorta(parole []string) int {

    count := 0

    for _, len := range parole {

        for _, letter := range len {

            if len < letter {

                parole = len

            }

        }

    }

return parole

}

but I know that there are some mistakes… anyone can help with some advices?

Seems as if len(s) (with s being a string) returns the same result as len([]byte(s)), but len([]rune(s)) returns a potentially smaller nummer, but never a bigger one.

1 Like

package main

import “fmt”

func main() {

const N = 10

parole := make([]string, N)

for i := 0; i < N; i++ {

    fmt.Scan(&parole[i])

}

fmt.Println(piuCorta(parole))

}

func piuCorta(parole []string) int {

    count := 0

    for _, len := range parole {

        for _, letter := range len {

            if len < letter {

                parole = len

                fmt.Printf("%#v has a lenght of %v bytes", count, len([]byte(s)))

            }

        }

    }

return parole

}

I’ve followed your istructions but idk why it gives me errors

What kind of errors?

And what are you actually trying to do?

Hi, to be honest I don;t understand what piuCorta is doing

with

for _, len := range parole {
    for _, letter := range len {
        if len < letter {
            ...
        }
    }
}

len is a string and letter is a rune. what are you trying to do ?

Ask yourself what are the types of len and letter and what you expect to be the result of the operation above. BTW, it is poor practice to reuse the names of builtin functions for variables.

1 Like

len([]rune(s)) gives the number of Unicode characters, len(s) gives number of bytes