Hi there, I was trying to do this exercise:
Write a program that receives from the command line a series of arbitrary length of words
and print the longest word. In case there are more words of maximum length, the program prints
the last. For example, if the user inserts the sequence of words one two four thirteen the program
press thirteen.
Note that:
- the data comes from the command line
- we have a sequence of data that can be processed in series, in a uniform manner
- these are strings
- All data entered at startup of the program are already stored in slice os.Args
I was writing code but idk how to set func main related to the arguments written in command line, I’ve written this code:
package main
import (
"fmt"
"os"
"strings"
)
func longestword(s string) (best string) {
for _, word:= range strings.Split(s, " ") {
if len(word) > len(best) {
best = word
}
}
return
}
func main() {
var arg string
for _, arg = range os.Args[1:] {
fmt.Println(arg)
}
}
Is there some funcion that identify the longest word?