Added support for m flag

main
oabrivard 2 years ago
parent 944f21ec5c
commit a0cabd874d

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

@ -1,12 +1,14 @@
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"slices"
"unicode/utf8"
)
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() {
cFlag := flag.Bool("c", false, "print the byte counts")
lFlag := flag.Bool("l", false, "print the line counts")
wFlag := flag.Bool("w", false, "print the word counts")
cFlag := flag.Bool("c", false, "print the byte count")
lFlag := flag.Bool("l", false, "print the line count")
wFlag := flag.Bool("w", false, "print the word count")
mFlag := flag.Bool("m", false, "print the character count")
flag.Parse()
fileName := flag.Arg(0)
@ -103,5 +125,7 @@ func main() {
lineCounter(f)
case *wFlag:
wordCounter(f)
case *mFlag:
charCounter(f)
}
}

Loading…
Cancel
Save