longest word
This commit is contained in:
parent
c1f98eb796
commit
59e20d36c1
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,3 +31,4 @@ w1d1ex2/w1d1ex2
|
||||
go-cat2/go-cat2
|
||||
lower-1line/lower-1line
|
||||
upper-words/upper-words
|
||||
longest-word/longest-word
|
||||
|
45
longest-word/main.go
Normal file
45
longest-word/main.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "io"
|
||||
import "log"
|
||||
import "os"
|
||||
import "bufio"
|
||||
import "strings"
|
||||
|
||||
func longest(r io.Reader) string {
|
||||
lw := ""
|
||||
s := bufio.NewScanner(r)
|
||||
s.Split(bufio.ScanWords)
|
||||
for s.Scan() {
|
||||
word := s.Text()
|
||||
word = strings.ToLower(word)
|
||||
word = strings.Replace(word, ".", "", -1)
|
||||
word = strings.Replace(word, ",", "", -1)
|
||||
word = strings.Replace(word, "\"", "", -1)
|
||||
for _, word := range strings.Split(word, "—") {
|
||||
if len(word) > len(lw) {
|
||||
if !strings.HasPrefix(word, "http://") {
|
||||
lw = word
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return lw
|
||||
}
|
||||
|
||||
func openFile(path *string) *os.File {
|
||||
f, err := os.Open(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
func main() {
|
||||
if len(os.Args) <= 1 {
|
||||
log.Fatalln("Missing file")
|
||||
}
|
||||
f := openFile(&os.Args[1])
|
||||
|
||||
fmt.Println(longest(f))
|
||||
}
|
Loading…
Reference in New Issue
Block a user