upperwords

This commit is contained in:
Meutel 2017-07-07 16:08:14 +02:00
parent 1aa7ac5d38
commit 675b497a70
2 changed files with 79 additions and 0 deletions

1
.gitignore vendored
View File

@ -30,3 +30,4 @@ swap/swap
w1d1ex2/w1d1ex2
go-cat2/go-cat2
lower-1line/lower-1line
upper-words/upper-words

78
upper-words/main.go Normal file
View File

@ -0,0 +1,78 @@
package main
import "bufio"
//import "fmt"
import "io"
import "log"
import "os"
import "strings"
func splitWord(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) > 0 {
//fmt.Print("EOF")
return len(data), data, nil
}
splitAt := -1
if len(data) > 0 {
for i, c := range data {
//fmt.Print(string(c))
switch string(c) {
case "\n":
fallthrough
case " ":
fallthrough
case "\t":
if splitAt < 0 {
splitAt = i
}
default:
if splitAt > 0 {
return i, data[:splitAt], nil
}
}
}
//fmt.Println()
}
return 0, nil, nil
}
func transform(in io.Reader, out io.Writer) {
bufw := bufio.NewWriter(out)
s := bufio.NewScanner(in)
//s.Split(bufio.ScanWords)
s.Split(splitWord)
for s.Scan() {
token := s.Text()
//fmt.Println(token)
if len(token) > 1 {
bufw.WriteString(strings.ToUpper(string(token[0])) + token[1:] + " ")
}
}
bufw.Flush()
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Usage upper-line <file>")
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatalln("Error opening file", err)
}
defer f.Close()
var w io.Writer
if len(os.Args) >= 3 {
fout, err := os.Create(os.Args[2])
if err != nil {
log.Fatalln("Error creating file", err)
}
defer fout.Close()
w = fout
} else {
w = os.Stdout
}
transform(f, w)
}