标签:字符 ati end stack package under The tst empty
给定一个字符串,逐个翻转字符串中的每个单词。例如,输入: "the sky is blue",输出: "blue is sky the"。
package main import ( "fmt" "strings" ) type SStack struct { elems []string } func (this SStack) Is_empty() bool { return len(this.elems) == 0 } func (this SStack) Top() string { if this.Is_empty() { panic("in SStack.top") } return this.elems[len(this.elems)-1] } func (this *SStack) Push(elem string) { this.elems = append(this.elems, elem) } func (this *SStack) Pop() string { if this.Is_empty() { panic("in SStack.pop") } elem := this.elems[len(this.elems)-1] this.elems = this.elems[:len(this.elems)-1] return elem } func main() { s := SStack{} str := "the sky is blue" ss := strings.Split(str, " ") for _, i := range ss { s.Push(i) } var resultStr string for !s.Is_empty() { resultStr += s.Pop() resultStr += " " } resultStr = strings.TrimSpace(resultStr) fmt.Println(resultStr) }
标签:字符 ati end stack package under The tst empty
原文地址:https://www.cnblogs.com/xiangxiaolin/p/13590533.html