How to move the first word to the end of a string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func MoveFirstWordToEndOfString(s string) string {
    parts := strings.Fields(s)
    if len(parts) <= 1 {
        return s
    }

    // Move the first word to the end
    first := parts[0]
    rest := parts[1:]

    return strings.Join(append(rest, first), " ")
}

func main() {
    s := "Would you like to know more? (Explore and learn)"
    result := MoveFirstWordToEndOfString(s)
    fmt.Println(result)
}



/*
run:

you like to know more? (Explore and learn) Would

*/

 



answered Feb 5 by avibootz
...