Reverse a string: Difference between revisions

→‎{{header|Go}}: extra credit
(→‎{{header|Go}}: extra credit)
Line 447:
<lang gema>\L<U1><U>=@{$2}$1</lang>
=={{header|Go}}==
<lang go>package main
No extra credit.
<lang go>
package main
 
import "fmt"(
"fmt"
"unicode"
)
 
func mainreverseBytes(s string) string {
r := make([]byte, len(s))
s := "Three quarks for muster mark!"
for i := 0; i < len(s); i++ {
r[i] = s[len(s)-1-i]
}
return string(r)
}
 
func reverseCodePoints(s string) string {
r := make([]int, len(s))
start := len(s)
Line 461 ⟶ 469:
r[start] = c
}
fmt.Println(return string(r[start:]))
}
 
</lang>
func reversePreservingCombiningCharacters(s string) string {
if s == "" {
return ""
}
p := []int(s)
r := make([]int, len(p))
for i := 0; i < len(p); {
j := i + 1
for j < len(p) && (unicode.Is(unicode.Mn, p[j]) ||
unicode.Is(unicode.Me, p[j]) || unicode.Is(unicode.Mc, p[j])) {
j++
}
for k := i; k < j; k++ {
 
r[len(r)-j+k-i] = p[k]
}
i = j
}
return (string(r))
}
 
func main() {
test("asdf")
test("as⃝df̅")
}
 
func test(s string) {
fmt.Println("\noriginal: ", []byte(s), s)
r := reverseBytes(s)
fmt.Println("reversed bytes:", []byte(r), r)
fmt.Println("original code points:", []int(s), s)
r = reverseCodePoints(s)
fmt.Println("reversed code points:", []int(r), r)
r = reversePreservingCombiningCharacters(s)
fmt.Println("combining characters:", []int(r), r)
}</lang go>
Output:
<pre>
original: [97 115 100 102] asdf
reversed bytes: [102 100 115 97] fdsa
original code points: [97 115 100 102] asdf
reversed code points: [102 100 115 97] fdsa
combining characters: [102 100 115 97] fdsa
 
original: [97 115 226 131 157 100 102 204 133] as⃝df̅
reversed bytes: [133 204 102 100 157 131 226 115 97] ��fd���sa
original code points: [97 115 8413 100 102 773] as⃝df̅
reversed code points: [773 102 100 8413 115 97] ̅fd⃝sa
combining characters: [102 773 100 115 8413 97] f̅ds⃝a
</langpre>
 
=={{header|Groovy}}==
1,707

edits