一个没有翻转单词的字符串。单词之间用点号分隔。

huangapple go评论74阅读模式
英文:

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 (
       &quot;fmt&quot;
       &quot;strings&quot;
    ) 
    func main() {
       str := &quot; i.like.this.program.very.much&quot;
       words := strings.Split(str, &quot; &quot;)
       len := len(words)
       for i := 0; i&lt;len/2; i++ {
          tmp := words[len-i-1]
          words[len-i-1] = words[i]
          words[i] = tmp
       }
       str = strings.Join(words,&quot; &quot;)
       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 (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {
	str := &quot;i.like.this.program.very.much&quot;
	res1 := strings.Split(str, &quot;.&quot;)
	revString := &quot;&quot;
	for i := len(res1) - 1; i &gt;= 0; i-- {
		revString += res1[i]
		if i != 0 {
			revString += &quot;.&quot;
		}
	}
	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 (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {
	s := &quot;i.like.this.program.very.much&quot;
	r := reverseWords(s)
	fmt.Println(s)
	fmt.Println(r)
}

func reverseWords(s string) string {
	words := strings.Split(s, &quot;.&quot;)
	reversedWords := reverse(words)
	r := strings.Join(reversedWords, &quot;.&quot;)
	return r
}

func reverse(arr []string) []string {
	for i, j := 0, len(arr)-1; i &lt; j; {
		arr[i], arr[j] = arr[j], arr[i]
		i++
		j--
	}
	return arr
}

huangapple
  • 本文由 发表于 2022年8月24日 12:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/73467366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定