Перевернуть строку

Messages
1,679
Reaction score
288
Website
tehadm.ru
C-like:
package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(ReverseWords("The greatest victory is that which requires no battle"))
    fmt.Println(ReverseWords("hello world!"))
}

func ReverseWords(str string) string {
    temp := ""
    newStr := strings.Split(str, " ")
    for i := len(newStr) - 1; i >= 0; i-- {
        temp += newStr[i] + " "
    }

    return strings.TrimSpace(temp)
}
 
C-like:
import "strings"

func ReverseWords(str string) (result string) {
  strs := strings.Fields(str)
  for _, value := range strs {
    result = value + " " + result
  }
  return result[:len(result) - 1]
}
 
Back
Top