157

How can we reverse a simple string in Go?

6
  • 2
    As far as I understand, the solutions given below don't work with precomposed or combining characters, like giving a+´ instead of á. I wonder how that could be taken into account, without normalizing it.
    – siritinga
    Sep 5, 2014 at 7:55
  • If you are confused with a huge number of similar answers, check my benchmark. Dec 30, 2015 at 0:22
  • 2
    For strings which are not simple, see stackoverflow.com/a/62743214/6309
    – VonC
    Dec 15, 2020 at 16:34
  • 4
    Seems like something apparently so hard should be in the standard library...but just another thing left out. Apr 1, 2021 at 14:59
  • as @dolmen noted many answers are not that great. I suggest the reader to check for stackoverflow.com/questions/53244824/… I also vote to close that question as dup. Not great for search engines, but correctness seems more important (that last pst can also be renamed).
    – user4466350
    Sep 21, 2021 at 21:45

37 Answers 37

154

In Go1 rune is a builtin type.

func Reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}
3
  • 6
    you cannot use len() in Go to find out the length of a string/Array/Slice etc... Here's why? - len() in Go means the size of the input in bytes. It does not correspond to its length. - Not all utf8's runes are of the same size. It can be either 1, 2, 4, or 8. - You should use unicode/ut8 package's method RuneCountInString to get the length of the rune. May 25, 2014 at 15:17
  • 38
    @AnveshChecka, that is incorrect. See golang.org/pkg/builtin/#len - len() on a slice definitely returns the number of elements, not the size in bytes. A slice of runes is the correct way to do it.
    – chowey
    Jan 17, 2015 at 2:53
  • 9
    @рытфолд This does not work with combining characters. See play.golang.org/p/sBgZAV7gCb, the combining character is not swapped with its base.
    – chowey
    Jan 17, 2015 at 2:59
66

Russ Cox, on the golang-nuts mailing list, suggests

package main 
import "fmt"
func main() { 
        input := "The quick brown 狐 jumped over the lazy 犬" 
        // Get Unicode code points. 
        n := 0
        rune := make([]rune, len(input))
        for _, r := range input { 
                rune[n] = r
                n++
        } 
        rune = rune[0:n]
        // Reverse 
        for i := 0; i < n/2; i++ { 
                rune[i], rune[n-1-i] = rune[n-1-i], rune[i] 
        } 
        // Convert back to UTF-8. 
        output := string(rune)
        fmt.Println(output)
}
14
  • 27
    I like how they force you to think about encodings. Nov 18, 2009 at 9:46
  • 22
    off-topic: why is it [golang-nuts] and not [go-nuts]?
    – Jimmy
    Mar 19, 2010 at 16:29
  • 2
    Wow, wtf is up with the double assignment when reversing? Interesting. Now, think about a string with an uneven number of runes. The middle one gets special treatment, with the correct end-result after all though. :) An interesting little optimization I wouldn’t have thought of right away.
    – Kissaki
    Jul 15, 2012 at 21:36
  • 5
    I don't understand why this conversion to rune, why not rune:=[]rune(input)?
    – siritinga
    Sep 5, 2014 at 7:31
  • 2
    @N4v: With this solution, rune is initialized to len(input), which is the number of bytes in input. But a rune can be 1, 2, 4 or 8 bytes in UTF-8. So if all the runes are 1 byte (ASCII) UTF-8 characters, the number of runes in input will be equal to the number of bytes. But if some characters are longer byte sequences, then the number of runes will be less than the number of bytes. rune = rune[0:n] trims excess 0-valued runes from the end of the slice, which would then be included in the reverse loop and wind up on the front of the output string. Jul 18, 2019 at 21:00
55

This works, without all the mucking about with functions:

func Reverse(s string) (result string) {
  for _,v := range s {
    result = string(v) + result
  }
  return 
}
8
  • 13
    While it works, since strings are immutable, it's very inefficient. I've posted a more efficient solution.
    – peterSO
    Feb 11, 2011 at 7:16
  • 7
    This is much too easy to understand. Make it harder :-) (and, "plus one" for just getting on with it)
    – Roboprog
    Apr 13, 2013 at 1:33
  • 3
    This is the best answer unless reversing strings is your bottleneck.
    – Banjocat
    Jun 15, 2015 at 17:47
  • 2
    One more wrong "solution" that doesn't handle combining characters properly.
    – dolmen
    Jul 26, 2016 at 12:42
  • 4
    @StanR. A rune is not a glyph. A glyph can be made of multiple codepoints/runes. See reedbeta.com/blog/programmers-intro-to-unicode/#combining-marks Reversing codepoints will attach combining marks to a different base code point.
    – dolmen
    Mar 16, 2017 at 23:14
24

From Go example projects: golang/example/stringutil/reverse.go, by Andrew Gerrand

/*
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
}

Go Playground for reverse a string

After reversing string "bròwn", the correct result should be "nwòrb", not "nẁorb".
Note the grave above the letter o.


For preserving Unicode combining characters such as "as⃝df̅" with reverse result "f̅ds⃝a",
please refer to another code listed below:

http://rosettacode.org/wiki/Reverse_a_string#Go

3
  • 2
    Thanks for clarifying the difference from stackoverflow.com/a/10030772/3093387 -- it seems that these two solutions differ in how they handle strings like "bròwn".
    – josliber
    Jan 11, 2016 at 19:21
  • 2
    Thanks for mentioning the Rosettacode solution that handles combining characters
    – dolmen
    Jul 26, 2016 at 12:56
  • Thank you for sharing this example
    – marti_
    May 18, 2021 at 14:10
21

There are too many answers here. Some of them are clear duplicates. But even from the left one, it is hard to select the best solution.

So I went through the answers, thrown away the one that does not work for unicode and also removed duplicates. I benchmarked the survivors to find the fastest. So here are the results with attribution (if you notice the answers that I missed, but worth adding, feel free to modify the benchmark):

Benchmark_rmuller-4   100000         19246 ns/op
Benchmark_peterSO-4    50000         28068 ns/op
Benchmark_russ-4       50000         30007 ns/op
Benchmark_ivan-4       50000         33694 ns/op
Benchmark_yazu-4       50000         33372 ns/op
Benchmark_yuku-4       50000         37556 ns/op
Benchmark_simon-4       3000        426201 ns/op

So here is the fastest method by rmuller:

func Reverse(s string) string {
    size := len(s)
    buf := make([]byte, size)
    for start := 0; start < size; {
        r, n := utf8.DecodeRuneInString(s[start:])
        start += n
        utf8.EncodeRune(buf[size-start:], r)
    }
    return string(buf)
}

For some reason I can't add a benchmark, so you can copy it from PlayGround (you can't run tests there). Rename it and run go test -bench=.

2
15

This works on unicode strings by considering 2 things:

  • range works on string by enumerating unicode characters
  • string can be constructed from int slices where each element is a unicode character.

So here it goes:

func reverse(s string) string {
    o := make([]int, utf8.RuneCountInString(s));
    i := len(o);
    for _, c := range s {
        i--;
        o[i] = c;
    }
    return string(o);
}
6
  • I would assign i:=len(o)-1 and then fold the for into a single line for _, c:=range s { o[i--]=c; }. Man I HATE the for without parentheses - is this allowed: for(_, c:=range s) { o[i--]=c; } Nov 19, 2009 at 19:43
  • Could you explain what the _ does? Nov 19, 2009 at 19:44
  • 7
    @Software_Monkey: o[i--] = c is not allowed in Go. -- and ++ are statements, not expressions. _ means to discard (ignore) that variable. Nov 25, 2009 at 7:10
  • 3
    with go 1.1+ it returns error in string([]int) line, if instead []rune type is used for o, all works
    – Otuk
    Aug 11, 2013 at 3:37
  • 2
    @yuku: Still fails on s := "Les Mise\u0301rables" Mar 30, 2016 at 14:19
14

I wrote the following Reverse function which respects UTF8 encoding and combined characters:

// Reverse reverses the input while respecting UTF8 encoding and combined characters
func Reverse(text string) string {
    textRunes := []rune(text)
    textRunesLength := len(textRunes)
    if textRunesLength <= 1 {
        return text
    }

    i, j := 0, 0
    for i < textRunesLength && j < textRunesLength {
        j = i + 1
        for j < textRunesLength && isMark(textRunes[j]) {
            j++
        }

        if isMark(textRunes[j-1]) {
            // Reverses Combined Characters
            reverse(textRunes[i:j], j-i)
        } 

        i = j
    }

    // Reverses the entire array
    reverse(textRunes, textRunesLength)

    return string(textRunes)
}

func reverse(runes []rune, length int) {
    for i, j := 0, length-1; i < length/2; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
}

// isMark determines whether the rune is a marker
func isMark(r rune) bool {
    return unicode.Is(unicode.Mn, r) || unicode.Is(unicode.Me, r) || unicode.Is(unicode.Mc, r)
}

I did my best to make it as efficient and readable as possible. The idea is simple, traverse through the runes looking for combined characters then reverse the combined characters' runes in-place. Once we have covered them all, reverse the runes of the entire string also in-place.

Say we would like to reverse this string bròwn. The ò is represented by two runes, one for the o and one for this unicode \u0301a that represents the "grave".

For simplicity, let's represent the string like this bro'wn. The first thing we do is look for combined characters and reverse them. So now we have the string br'own. Finally, we reverse the entire string and end up with nwo'rb. This is returned to us as nwòrb

You can find it here https://github.com/shomali11/util if you would like to use it.

Here are some test cases to show a couple of different scenarios:

func TestReverse(t *testing.T) {
    assert.Equal(t, Reverse(""), "")
    assert.Equal(t, Reverse("X"), "X")
    assert.Equal(t, Reverse("b\u0301"), "b\u0301")
    assert.Equal(t, Reverse("😎⚽"), "⚽😎")
    assert.Equal(t, Reverse("Les Mise\u0301rables"), "selbare\u0301siM seL")
    assert.Equal(t, Reverse("ab\u0301cde"), "edcb\u0301a")
    assert.Equal(t, Reverse("This `\xc5` is an invalid UTF8 character"), "retcarahc 8FTU dilavni na si `�` sihT")
    assert.Equal(t, Reverse("The quick bròwn 狐 jumped over the lazy 犬"), "犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT")
}
2
  • 3
    So far this is the only answer that works with a string that includes grapheme clustered chars (combined chars) such as 🙏2️⃣👌👁 which reverses to 👁👌2️⃣🙏. Most of the other answers return 👁👌⃣️2🙏. paiza.io/projects/K8_kOmxn-cGRR3ksh1vFtw
    – KEINOS
    Oct 16, 2020 at 4:37
  • 1
    While certainly better than the other implementations, it only works with a small subset of grapheme clusters possible according to the Unicode specification. For example, 🇩🇪🏳️‍🌈 reverses to 🌈‍🏳️🇪🇩 and 👨‍👩‍👧‍👦 reverses to 👦‍👧‍👩‍👨. If you care about correct Unicode handling, you should use: pkg.go.dev/github.com/rivo/uniseg#ReverseString
    – Oliver
    Nov 15, 2022 at 18:24
13

I noticed this question when Simon posted his solution which, since strings are immutable, is very inefficient. The other proposed solutions are also flawed; they don't work or they are inefficient.

Here's an efficient solution that works, except when the string is not valid UTF-8 or the string contains combining characters.

package main

import "fmt"

func Reverse(s string) string {
    n := len(s)
    runes := make([]rune, n)
    for _, rune := range s {
        n--
        runes[n] = rune
    }
    return string(runes[n:])
}

func main() {
    fmt.Println(Reverse(Reverse("Hello, 世界")))
    fmt.Println(Reverse(Reverse("The quick brown 狐 jumped over the lazy 犬")))
}
5
  • 1
    return string(runes) Works as well.
    – user52898
    Jan 6, 2013 at 21:53
  • 3
    @Tommy: No, return string(runes) does not work for all cases.
    – peterSO
    Aug 19, 2013 at 20:01
  • could you please explain a bit more on why that is? I made a short program and it works there, but maybe those cases you talk about aren't triggered there? play.golang.org/p/yk1sAwFjol
    – user52898
    Aug 20, 2013 at 9:52
  • 1
    @Tommy: Your short program merely demonstrates that the NUL character is a NOP when sent to a printer or terminal. Your Reverse2 function fails for non-ASCII UTF-8 encoded strings. I've revised your short program so that it's a valid test: play.golang.org/p/Ic5G5QEO93
    – peterSO
    Aug 21, 2013 at 2:19
  • 1
    One more wrong "solution" that doesn't handle combining characters properly.
    – dolmen
    Jul 26, 2016 at 12:42
12
//Reverse reverses string using strings.Builder. It's about 3 times faster
//than the one with using a string concatenation
func Reverse(in string) string {
    var sb strings.Builder
    runes := []rune(in)
    for i := len(runes) - 1; 0 <= i; i-- {
        sb.WriteRune(runes[i])
    }
    return sb.String()
}


//Reverse reverses string using string
func Reverse(in string) (out string) {
    for _, r := range in {
        out = string(r) + out
    }
    return
}

BenchmarkReverseStringConcatenation-8   1000000 1571 ns/op  176 B/op    29 allocs/op
BenchmarkReverseStringsBuilder-8        3000000 499 ns/op   56 B/op 6 allocs/op

Using strings.Builder is about 3 times faster than using string concatenation

3
  • 1
    I wonder, why this question has no upvotes despite being most accurate answer
    – Nilesh
    Jun 24, 2020 at 13:28
  • 1
    it doesn't work for all cases e.g. "Les Mise\u0301rables"
    – matino
    Jun 14, 2021 at 7:56
  • Doesn't work: bròwn becomes nẁorb. See: go.dev/play/p/MRyIb0fYQ6y
    – prnsml
    Aug 16, 2023 at 16:03
9

A simple stroke with rune:

func ReverseString(s string) string {
    runes := []rune(s)
    size := len(runes)
    for i, j := 0, size-1; i < size>>1; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

func main() {
    fmt.Println(ReverseString("Abcdefg 汉语 The God"))
}
: doG ehT 语汉 gfedcbA
3
  • using i,j 2 indices might be better, and size/2 can be size >> 1.
    – Eric
    May 4, 2023 at 2:28
  • > using i,j 2 indices might be better, and size/2 can be size >> 1 Updated
    – Eric
    May 5, 2023 at 3:21
  • for i < j is more common/readable instead of i < size >> 1
    – Xeoncross
    Jul 19, 2023 at 1:50
6

Here is quite different, I would say more functional approach, not listed among other answers:

func reverse(s string) (ret string) {
    for _, v := range s {
        defer func(r rune) { ret += string(r) }(v)
    }
    return
}
4
  • I'm pretty sure it is not the fastest solution, but it shows how return variable ret is kept in closure for further processing, by each defer function. Jun 29, 2016 at 14:29
  • 2
    Slow and doesn't handle combining characters properly.
    – dolmen
    Jul 26, 2016 at 12:59
  • 1
    I'm not sure how fast that is, but it's beautiful.
    – donatJ
    Sep 15, 2016 at 21:51
  • Performance of this one may be improved in Go 1.14. At least release notes claims to have zero overhead of defer. Jan 2, 2020 at 12:17
6
func ReverseString(str string) string {
  output :=""
  for _, char := range str {
    output = string(char) + output
  }
  return output
}

// "Luizpa" -> "apziuL"
// "123日本語" -> "語本日321"
// "⚽😎" -> "😎⚽"
// "´a´b´c´" -> "´c´b´a´"
1
4

This is the fastest implementation

func Reverse(s string) string {
    size := len(s)
    buf := make([]byte, size)
    for start := 0; start < size; {
        r, n := utf8.DecodeRuneInString(s[start:])
        start += n
        utf8.EncodeRune(buf[size-start:], r)
    }
    return string(buf)
}

const (
    s       = "The quick brown 狐 jumped over the lazy 犬"
    reverse = "犬 yzal eht revo depmuj 狐 nworb kciuq ehT"
)

func TestReverse(t *testing.T) {
    if Reverse(s) != reverse {
        t.Error(s)
    }
}

func BenchmarkReverse(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Reverse(s)
    }
}
4
  • Did you benchmark the solutions before claiming it's the fastest implementation ? Sep 5, 2014 at 7:25
  • yes i did, that is why the BenchmarkReverse code is present :) . However i do not have the results anymore.
    – rmuller
    Sep 23, 2014 at 17:30
  • 3
    Fast solution, but still wrong as it doesn't handle combining characters properly.
    – dolmen
    Jul 26, 2016 at 12:58
  • Is it true as @dolmen says that this does not handle combining characters? Is there a solution here that does?
    – geraldss
    Oct 13, 2016 at 18:14
3

You could also import an existing implementation:

import "4d63.com/strrev"

Then:

strrev.Reverse("abåd") // returns "dåba"

Or to reverse a string including unicode combining characters:

strrev.ReverseCombining("abc\u0301\u031dd") // returns "d\u0301\u031dcba"

These implementations supports correct ordering of unicode multibyte and combing characters when reversed.

Note: Built-in string reverse functions in many programming languages do not preserve combining, and identifying combining characters requires significantly more execution time.

2

This code preserves sequences of combining characters intact, and should work with invalid UTF-8 input too.

package stringutil
import "code.google.com/p/go.text/unicode/norm"

func Reverse(s string) string {
    bound := make([]int, 0, len(s) + 1)

    var iter norm.Iter
    iter.InitString(norm.NFD, s)
    bound = append(bound, 0)
    for !iter.Done() {
        iter.Next()
        bound = append(bound, iter.Pos())
    }
    bound = append(bound, len(s))
    out := make([]byte, 0, len(s))
    for i := len(bound) - 2; i >= 0; i-- {
        out = append(out, s[bound[i]:bound[i+1]]...)
    }
    return string(out)
}

It could be a little more efficient if the unicode/norm primitives allowed iterating through the boundaries of a string without allocating. See also https://code.google.com/p/go/issues/detail?id=9055 .

4
  • There is no such "invalid UTF-8 input" in string values: when converting from []byte to string Go replaces "invalid UTF-8 input" by a valid codepoint \uFFFD.
    – dolmen
    Apr 25, 2017 at 15:36
  • I don't understand the above comment. Are you saying that the behaviour of this code is wrong when presented with a string containing invalid UTF-8 ?
    – rog
    Apr 26, 2017 at 16:25
  • No. I'm saying that invalid UTF-8 in a Go string doesn't exist. But it can exist in a []byte.
    – dolmen
    May 2, 2017 at 14:04
  • A Go string can contain exactly as much invalid utf-8 as a []byte. For example: play.golang.org/p/PG0I4FJfEN
    – rog
    May 3, 2017 at 15:02
2

If you need to handle grapheme clusters, use unicode or regexp module.

package main

import (
  "unicode"
  "regexp"
)

func main() {
    str := "\u0308" + "a\u0308" + "o\u0308" + "u\u0308"
    println("u\u0308" + "o\u0308" + "a\u0308" + "\u0308" == ReverseGrapheme(str))
    println("u\u0308" + "o\u0308" + "a\u0308" + "\u0308" == ReverseGrapheme2(str))
}

func ReverseGrapheme(str string) string {

  buf := []rune("")
  checked := false
  index := 0
  ret := "" 

    for _, c := range str {

        if !unicode.Is(unicode.M, c) {

            if len(buf) > 0 {
                ret = string(buf) + ret
            }

            buf = buf[:0]
            buf = append(buf, c)

            if checked == false {
                checked = true
            }

        } else if checked == false {
            ret = string(append([]rune(""), c)) + ret
        } else {
            buf = append(buf, c)
        }

        index += 1
    }

    return string(buf) + ret
}

func ReverseGrapheme2(str string) string {
    re := regexp.MustCompile("\\PM\\pM*|.")
    slice := re.FindAllString(str, -1)
    length := len(slice)
    ret := ""

    for i := 0; i < length; i += 1 {
        ret += slice[length-1-i]
    }

    return ret
}
2
  • I'd like to give you 1'000 upvotes. All other implementations on this page reverse a STRING incorrectly (a STRING is NOT a sequence of chars). Mar 30, 2016 at 14:26
  • This doesn't work. If you double reverse the string you do not get the original string. The leading Combining Diaeresis (\u0308), used in this example combines with preceding characters creating a double umlaut 'a' when reversed. If str is output quoted it modifies the leading quote! Jan 26, 2018 at 5:26
2

It's assuredly not the most memory efficient solution, but for a "simple" UTF-8 safe solution the following will get the job done and not break runes.

It's in my opinion the most readable and understandable on the page.

func reverseStr(str string) (out string) {
    for _, s := range str {
        out = string(s) + out
    }

    return
}
2

The following two methods run faster than the fastest solution that preserve combining characters, though that's not to say I'm missing something in my benchmark setup.

//input string s
bs := []byte(s)
var rs string
for len(bs) > 0 {
    r, size := utf8.DecodeLastRune(bs)
    rs += fmt.Sprintf("%c", r)
    bs = bs[:len(bs)-size]
} // rs has reversed string

Second method inspired by this

//input string s
bs := []byte(s)
cs := make([]byte, len(bs))
b1 := 0
for len(bs) > 0 {
    r, size := utf8.DecodeLastRune(bs)
    d := make([]byte, size)
    _ = utf8.EncodeRune(d, r)
    b1 += copy(cs[b1:], d)
    bs = bs[:len(bs) - size]
} // cs has reversed bytes
2
  • 2
    Here is what you are missing in your benchmark: your solution is faster because it doesn't preserve combining characters. It is just unfair to compare them.
    – dolmen
    Apr 25, 2017 at 15:48
  • Apart from not handling combined characters, this answer is so far the fastest in this thread. It is the only one that avoids doing 2 allocations: one per rune slice, one to make a string back out of it Apr 3, 2022 at 19:26
2

Simple, Sweet and Performant

func reverseStr(str string) string {
  strSlice := []rune(str)  //converting to slice of runes
  length := len(strSlice)

  for i := 0; i < (length / 2); i++ {
      strSlice[i], strSlice[length-i-1] = strSlice[length-i-1], strSlice[i]
  }
  return string(strSlice)  //converting back to string
}
1

NOTE: This answer is from 2009, so there are probably better solutions out there by now.


Looks a bit 'roundabout', and probably not very efficient, but illustrates how the Reader interface can be used to read from strings. IntVectors also seem very suitable as buffers when working with utf8 strings.

It would be even shorter when leaving out the 'size' part, and insertion into the vector by Insert, but I guess that would be less efficient, as the whole vector then needs to be pushed back by one each time a new rune is added.

This solution definitely works with utf8 characters.

package main

import "container/vector";
import "fmt";
import "utf8";
import "bytes";
import "bufio";


func
main() {
    toReverse := "Smørrebrød";
    fmt.Println(toReverse);
    fmt.Println(reverse(toReverse));
}

func
reverse(str string) string {
    size := utf8.RuneCountInString(str);
    output := vector.NewIntVector(size);
    input := bufio.NewReader(bytes.NewBufferString(str));
    for i := 1; i <= size; i++ {
        rune, _, _ := input.ReadRune();
        output.Set(size - i, rune);
    }
    return string(output.Data());
}
4
  • 1
    Why do you add all those trailing semicolons?
    – Morteza R
    Oct 26, 2015 at 20:34
  • 2
    @olivier-mason It's time to learn about gofmt when sharing Go code.
    – dolmen
    Apr 25, 2017 at 15:30
  • 2
    That answer was from eight years ago. Apr 26, 2017 at 12:32
  • 3
    @OliverMason It's never too late to fix (or delete) an imperfect solution.
    – dolmen
    Jun 22, 2018 at 20:36
1
func Reverse(s string) string {
    r := []rune(s)
    var output strings.Builder
    for i := len(r) - 1; i >= 0; i-- {
        output.WriteString(string(r[i]))
    }

    return output.String()
}
1
  • 2
    Thanks for the answer, but with 32 other answers and some that are virtually identical, does this really add anything new to the discourse? If so, please justify it with some explanation of why it's useful. Code-only answers are considered low-quality.
    – ggorlen
    Oct 11, 2020 at 2:10
1

Reversing a string by word is a similar process. First, we convert the string into an array of strings where each entry is a word. Next, we apply the normal reverse loop to that array. Finally, we smush the results back together into a string that we can return to the caller.

package main

import (
    "fmt"
    "strings"
)

func reverse_words(s string) string {
    words := strings.Fields(s)
    for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 {
        words[i], words[j] = words[j], words[i]
    }
    return strings.Join(words, " ")
}

func main() {
    fmt.Println(reverse_words("one two three"))
}
1

Another hack is to use built-in language features, for example, defer:

package main

import "fmt"

func main() {
    var name string
    fmt.Scanln(&name)

    for _, char := range []rune(name) {
        defer fmt.Printf("%c", char) // <-- LIFO does it all for you
    }
}
2
1

For simple strings it possible to use such construction:

func Reverse(str string) string {
    if str != "" {
        return Reverse(str[1:]) + str[:1]
    }
    return ""   
}

For Unicode strings it might look like this:

func RecursiveReverse(str string) string {
    if str == "" {
        return ""
    }
    runes := []rune(str)
    return RecursiveReverse(string(runes[1:])) + string(runes[0])
}
1
  • Wouldn't this create extra memory ? Its not in-place isn't it ? May 21, 2022 at 13:55
1

Recognising unicode code points is one thing, but how about grapheme clusters?

For example, 🏴‍☠️ is composed from 4 unicode code points / runes, and reversing such a string on the basis of runes will produce garbage. Not good.

Read more about Unicode segmentation in rivo/uniseg docs.

Here's the reversal of a string, using this package:

func reverseSlice[T any](s []T) {
    for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
        s[i], s[j] = s[j], s[i]
    }
}

func reverse(s string) string {
    var grStrings []string

    gr := uniseg.NewGraphemes(s)
    for gr.Next() {
        grStrings = append(grStrings, gr.Str())
    }

    reverseSlice(grStrings)

    return strings.Join(grStrings, "")
}
1
  • 1
    That package has a ReverseString function Feb 26 at 17:39
0

A version which I think works on unicode. It is built on the utf8.Rune functions:

func Reverse(s string) string {
    b := make([]byte, len(s));
    for i, j := len(s)-1, 0; i >= 0; i-- {
        if utf8.RuneStart(s[i]) {
            rune, size := utf8.DecodeRuneInString(s[i:len(s)]);
            utf8.EncodeRune(rune, b[j:j+size]);
            j += size;
        }
    }
    return string(b);
}
0

rune is a type, so use it. Moreover, Go doesn't use semicolons.

func reverse(s string) string {
    l := len(s)
    m := make([]rune, l)

    for _, c := range s {
        l--
        m[l] = c
    }
    return string(m)
}

func main() {
    str := "the quick brown 狐 jumped over the lazy 犬"
    fmt.Printf("reverse(%s): [%s]\n", str, reverse(str))
}
2
  • It used semicolons when that question was posted.
    – OneOfOne
    Dec 2, 2014 at 21:50
  • 1
    One more wrong "solution" that doesn't handle combining characters properly.
    – dolmen
    Jul 26, 2016 at 12:57
0

try below code:

package main

import "fmt"

func reverse(s string) string {
    chars := []rune(s)
    for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
        chars[i], chars[j] = chars[j], chars[i]
    }
    return string(chars)
}

func main() {
    fmt.Printf("%v\n", reverse("abcdefg"))
}

for more info check http://golangcookbook.com/chapters/strings/reverse/
and http://www.dotnetperls.com/reverse-string-go

0
    func reverseString(someString string) string {
        runeString := []rune(someString)
        var reverseString string
        for i := len(runeString)-1; i >= 0; i -- {
            reverseString += string(runeString[i])
        }
        return reverseString
    }
0

Strings are immutable object in golang, unlike C inplace reverse is not possible with golang. With C , you can do something like,

void reverseString(char *str) {
  int length = strlen(str)
  for(int i = 0, j = length-1; i < length/2; i++, j--)
  {
    char tmp = str[i];
    str[i] = str[j];
    str[j] = tmp;
  }
}

But with golang, following one, uses byte to convert the input into bytes first and then reverses the byte array once it is reversed, convert back to string before returning. works only with non unicode type string.

package main

import "fmt"

func main() {
    s := "test123 4"
    fmt.Println(reverseString(s))
}

func reverseString(s string) string {
    a := []byte(s)
    for i, j := 0, len(s)-1; i < j; i++ {
        a[i], a[j] = a[j], a[i]
        j--
    }
    return string(a)
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.