For Loop iteration over string slice doesn't w

2019-02-20 03:39发布

I wrote this code, which should translate a lower case english phrase into pig latin.

package main

import (
    "fmt"
    "strings"
    "bufio"
    "github.com/stretchr/stew/slice"
    "regexp"
    "os"
)

func main() {
    lst := []string{"sh", "gl", "ch", "ph", "tr", "br", "fr", "bl", "gr", "st", "sl", "cl", "pl", "fl", "th"}
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Type what you would like translated into pig-latin and press ENTER: ")
    sentenceraw, _ := reader.ReadString('\n')
    sentence := strings.Split(sentenceraw, " ")
    isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString
    newsentence := make([]string, 0)
    for _, i := range sentence {
        if slice.Contains([]byte{'a', 'e', 'i', 'o', 'u'}, i[0]) {
            newsentence = append(newsentence, strings.Join([]string{string(i), "ay"}, ""))
        } else if slice.Contains(lst, string(i[0])+string(i[1])) {
            newsentence = append(newsentence, strings.Join([]string{string(i[2:]), string(i[:2]), "ay"}, ""))
        } else if !isAlpha(string(i)) {
            newsentence = append(newsentence, strings.Join([]string{string(i)}, ""))
        } else {
            newsentence = append(newsentence, strings.Join([]string{string(i[1:]), string(i[0]), "ay"}, ""))
        }
    }
    fmt.Println(strings.Join(newsentence, " "))
}

However, it fails to do anything to the last word in the phrase.

If I use the sentence "the quick brown fox jumped over the lazy dog" I get "ethay uickqay ownbray oxfay umpedjay overay ethay azylay dog"

Everything here is right, except the last word! why doesn't it work? The same thing happens if I use "hello world" as my phrase.

标签: go slice
1条回答
贼婆χ
2楼-- · 2019-02-20 04:04

Package bufio

func (*Reader) ReadString

func (b *Reader) ReadString(delim byte) (string, error)

ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim.


returning a string containing the data up to and including the delimiter.

Get rid of any trailing newlines: "\n" or "\r\n". For a quick fix, write:

sentence := strings.Split(strings.TrimSpace(sentenceraw), " ")

For example,

package main

import (
    "bufio"
    "fmt"
    "os"
    "regexp"
    "strings"

    "github.com/stretchr/stew/slice"
)

func main() {
    lst := []string{"sh", "gl", "ch", "ph", "tr", "br", "fr", "bl", "gr", "st", "sl", "cl", "pl", "fl", "th"}
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Type what you would like translated into pig-latin and press ENTER: ")
    sentenceraw, _ := reader.ReadString('\n')
    sentence := strings.Split(strings.TrimSpace(sentenceraw), " ")
    isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString
    newsentence := make([]string, 0)
    for _, i := range sentence {
        if slice.Contains([]byte{'a', 'e', 'i', 'o', 'u'}, i[0]) {
            newsentence = append(newsentence, strings.Join([]string{string(i), "ay"}, ""))
        } else if slice.Contains(lst, string(i[0])+string(i[1])) {
            newsentence = append(newsentence, strings.Join([]string{string(i[2:]), string(i[:2]), "ay"}, ""))
        } else if !isAlpha(string(i)) {
            newsentence = append(newsentence, strings.Join([]string{string(i)}, ""))
        } else {
            newsentence = append(newsentence, strings.Join([]string{string(i[1:]), string(i[0]), "ay"}, ""))
        }
    }
    fmt.Println(strings.Join(newsentence, " "))
}

Output:

Type what you would like translated into pig-latin and press ENTER: hello world
ellohay orldway
查看更多
登录 后发表回答