Skip to content

Instantly share code, notes, and snippets.

@fupslot
Created April 5, 2021 14:28
Show Gist options
  • Save fupslot/c1072a128125d722fb244823ff6e132f to your computer and use it in GitHub Desktop.
Save fupslot/c1072a128125d722fb244823ff6e132f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
st := []byte("The quick brown fox jumps over the lazy dog")
l := len(st)
/**
* The idea is to iterate over the half of a string and exchange
* characters from one end to another.
* "stash" variable used to store a character of an iteration
*
* stash
* / / ^ ^
* / / \ \
* h | e | l | l | o
*/
// Devide the string lenght on half (sort of)
it := l >> 1 // faster devision ?
fmt.Printf("Iterations: %d\n", it)
var stash byte
for i := it - 1; i >= 0; i-- {
stash = st[i] // stashing
st[i] = st[l-1-i] // last become first
st[l-1-i] = stash // first become last
}
fmt.Printf("Result: %s", st)
// Result
// Iterations: 21
// Result: god yzal eht revo spmuj xof nworb kciuq ehT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment