Added support for m flag

main
oabrivard 2 years ago
parent 944f21ec5c
commit a0cabd874d

@ -0,0 +1 @@
דניאל

@ -1,12 +1,14 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"os" "os"
"slices" "slices"
"unicode/utf8"
) )
func byteCounter(f *os.File) { func byteCounter(f *os.File) {
@ -75,11 +77,31 @@ func wordCounter(f io.Reader) {
} }
} }
func charCounter(f *os.File) {
var line string
var err error
r := bufio.NewReader(f)
count := 0
for err == nil {
line, err = r.ReadString('\n')
count += utf8.RuneCountInString(line)
}
if err != io.EOF {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
fmt.Println(count)
}
func main() { func main() {
cFlag := flag.Bool("c", false, "print the byte counts") cFlag := flag.Bool("c", false, "print the byte count")
lFlag := flag.Bool("l", false, "print the line counts") lFlag := flag.Bool("l", false, "print the line count")
wFlag := flag.Bool("w", false, "print the word counts") wFlag := flag.Bool("w", false, "print the word count")
mFlag := flag.Bool("m", false, "print the character count")
flag.Parse() flag.Parse()
fileName := flag.Arg(0) fileName := flag.Arg(0)
@ -103,5 +125,7 @@ func main() {
lineCounter(f) lineCounter(f)
case *wFlag: case *wFlag:
wordCounter(f) wordCounter(f)
case *mFlag:
charCounter(f)
} }
} }

Loading…
Cancel
Save