英文:
A string without reversing words. Words are separated by dots
问题
像这样-在go语言中使用go语言(使用for循环和range函数)
给定一个字符串S,反转字符串而不反转其各个单词。单词之间用点分隔。
输入:i.like.this.program.very.much
输出:much.very.program.this.like.i
package main
import (
"fmt"
"strings"
)
func main() {
str := " i.like.this.program.very.much"
words := strings.Split(str, " ")
len := len(words)
for i := 0; i<len/2; i++ {
tmp := words[len-i-1]
words[len-i-1] = words[i]
words[i] = tmp
}
str = strings.Join(words," ")
fmt.Println(str)
}
英文:
Like this-Using go lang in go lang using (for loop and range fun)
Given a String S, reverse the string without reversing its individual words. Words are separated by dots.
Input : i.like.this.program.very.much
Output: much.very.program.this.like.i
package main
import (
"fmt"
"strings"
)
func main() {
str := " i.like.this.program.very.much"
words := strings.Split(str, " ")
len := len(words)
for i := 0; i<len/2; i++ {
tmp := words[len-i-1]
words[len-i-1] = words[i]
words[i] = tmp
}
str = strings.Join(words," ")
fmt.Println(str)
}
答案1
得分: 0
请注意,我是一个语言模型,无法直接运行代码。以下是您提供的代码的翻译:
package main
import (
"fmt"
"strings"
)
func main() {
str := "i.like.this.program.very.much"
res1 := strings.Split(str, ".")
revString := ""
for i := len(res1) - 1; i >= 0; i-- {
revString += res1[i]
if i != 0 {
revString += "."
}
}
fmt.Print(revString)
}
这段代码的功能是将字符串i.like.this.program.very.much
按照.
进行分割,并将分割后的字符串逆序拼接起来。最终输出结果为much.very.program.this.like.i
。
英文:
Here you go
package main
import (
"fmt"
"strings"
)
func main() {
str := "i.like.this.program.very.much"
res1 := strings.Split(str, ".")
revString := ""
for i := len(res1) - 1; i >= 0; i-- {
revString += res1[i]
if i != 0 {
revString += "."
}
}
fmt.Print(revString)
}
答案2
得分: -1
package main
import (
"fmt"
"strings"
)
func main() {
s := "i.like.this.program.very.much"
r := reverseWords(s)
fmt.Println(s)
fmt.Println(r)
}
func reverseWords(s string) string {
words := strings.Split(s, ".")
reversedWords := reverse(words)
r := strings.Join(reversedWords, ".")
return r
}
func reverse(arr []string) []string {
for i, j := 0, len(arr)-1; i < j; {
arr[i], arr[j] = arr[j], arr[i]
i++
j--
}
return arr
}
类似这样的代码:
https://goplay.tools/snippet/EEFOAYGfQ6z
package main
import (
"fmt"
"strings"
)
func main() {
s := "i.like.this.program.very.much"
r := reverseWords(s)
fmt.Println(s)
fmt.Println(r)
}
func reverseWords(s string) string {
words := strings.Split(s, ".")
reversedWords := reverse(words)
r := strings.Join(reversedWords, ".")
return r
}
func reverse(arr []string) []string {
for i, j := 0, len(arr)-1; i < j; {
arr[i], arr[j] = arr[j], arr[i]
i++
j--
}
return arr
}
英文:
Something like this:
https://goplay.tools/snippet/EEFOAYGfQ6z
package main
import (
"fmt"
"strings"
)
func main() {
s := "i.like.this.program.very.much"
r := reverseWords(s)
fmt.Println(s)
fmt.Println(r)
}
func reverseWords(s string) string {
words := strings.Split(s, ".")
reversedWords := reverse(words)
r := strings.Join(reversedWords, ".")
return r
}
func reverse(arr []string) []string {
for i, j := 0, len(arr)-1; i < j; {
arr[i], arr[j] = arr[j], arr[i]
i++
j--
}
return arr
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论