diff --git a/hebrew.txt b/hebrew.txt new file mode 100644 index 0000000..37ecc86 --- /dev/null +++ b/hebrew.txt @@ -0,0 +1 @@ +דניאל diff --git a/main.go b/main.go index d783753..f2f1e66 100644 --- a/main.go +++ b/main.go @@ -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) } }