| 12345678910111213141516171819202122232425262728 |
- package main
- import (
- "bufio"
- "fmt"
- "io"
- "os"
- )
- func main() {
- // Calling the count function to count the number of words
- // received from the standard input and printing it out
- fmt.Println(count(os.Stdin))
- }
- func count(r io.Reader) int {
- scanner := bufio.NewScanner(r)
- // Split sets the split function for the Scanner.
- // The default split function is ScanLines.
- scanner.Split(bufio.ScanWords)
- wc := 0
- // For every word scanned, increment the counter
- for scanner.Scan() {
- wc++
- }
- return wc
- }
|