Solution Review: Analyzing Input from Keyboard

This lesson discusses the solution to the challenge given in the previous lesson.

package main
import (
	"fmt"
	"bufio"
	"os"
	"strings"
)
var nrchars, nrwords, nrlines int

func main() {
	nrchars, nrwords, nrlines = 0, 0, 0 
	inputReader := bufio.NewReader(os.Stdin)
	fmt.Println("Please enter some input, type S in the new line to stop: ")
	for {
		input, err := inputReader.ReadString('\n')
		if err != nil {
			fmt.Printf("An error occurred: %s\n", err)
			return
		}
		if input == "S\n" { // Windows it is "S\r\n", on Linux it is "S\n"
			fmt.Println("Here are the counts:")
			fmt.Printf("Number of characters: %d\n", nrchars)
			fmt.Printf("Number of words: %d\n", nrwords)
			fmt.Printf("Number of lines: %d\n", nrlines)
			os.Exit(0)
		}
		Counters(input)
	}
}

func Counters(input string) {
	nrchars += len(input) - 2 // -2 for \r\n
	// count number of spaces, nr of words is +1
	nrwords += strings.Count(input, " ") + 1
	nrlines++
}

Get hands-on with 1400+ tech skills courses.