lower 1st car of line

This commit is contained in:
Meutel 2017-07-07 15:27:48 +02:00
parent 51f746907b
commit 1aa7ac5d38
2 changed files with 47 additions and 0 deletions

1
.gitignore vendored
View File

@ -29,3 +29,4 @@ w1d2ex1/w1d2ex1
swap/swap
w1d1ex2/w1d1ex2
go-cat2/go-cat2
lower-1line/lower-1line

46
lower-1line/main.go Normal file
View File

@ -0,0 +1,46 @@
package main
import "bufio"
import "io"
import "log"
import "os"
import "strings"
func transform(in io.Reader, out io.Writer) {
bufw := bufio.NewWriter(out)
s := bufio.NewScanner(in)
for s.Scan() {
line := s.Text()
for i, c := range line {
if i == 0 {
bufw.WriteString(strings.ToLower(string(c)))
} else {
bufw.WriteString(string(c))
}
}
bufw.WriteString("\n")
}
}
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 {
w, err = os.Create(os.Args[2])
if err != nil {
log.Fatalln("Error creating file", err)
}
} else {
w = os.Stdout
}
transform(f, w)
}