main.go 512 B

12345678910111213141516171819202122232425262728
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. )
  8. func main() {
  9. // Calling the count function to count the number of words
  10. // received from the standard input and printing it out
  11. fmt.Println(count(os.Stdin))
  12. }
  13. func count(r io.Reader) int {
  14. scanner := bufio.NewScanner(r)
  15. // Split sets the split function for the Scanner.
  16. // The default split function is ScanLines.
  17. scanner.Split(bufio.ScanWords)
  18. wc := 0
  19. // For every word scanned, increment the counter
  20. for scanner.Scan() {
  21. wc++
  22. }
  23. return wc
  24. }