View Problem

Reverse the characters in a string

Given the string "reverse me", produce the string "em esrever"
DiskEdit
ruby
puts "reverse me".reverse
ExpandDiskEdit
java
String reverse = new StringBuffer("reverse me").reverse().toString();
ExpandDiskEdit
java 1.5 or later
String reverse = new StringBuilder("reverse me").reverse().toString();
ExpandDiskEdit
java org.apache.commons
String reverse = StringUtils.reverse("reverse me");
DiskEdit
perl
$_ = reverse "reverse me"; print
ExpandDiskEdit
groovy
reversed = "reverse me".reverse()
ExpandDiskEdit
scala 2.8+
val reversed = "reverse me".reverse
DiskEdit
python
"reverse me"[::-1]
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ s = "reverse me";
array<Char>^ sa = s->ToCharArray();
Array::Reverse(sa);
String^ sr = gcnew String(sa);
ExpandDiskEdit
cpp
std::string s = "reverse me";
std::reverse(s.begin(), s.end());
ExpandDiskEdit
cpp
std::string s = "reverse me";
std::string sr(s.rbegin(), s.rend());
ExpandDiskEdit
cpp
std::string s = "reverse me";
std::swap_ranges(s.begin(), (s.begin() + s.size() / 2), s.rbegin());
ExpandDiskEdit
fsharp
let reversed = new String (Array.rev ("reverse me".ToCharArray()))
DiskEdit
fsharp F# 2.0 Interactive Build 4.0.30319.1
let word = "reverse me"
//reverse the word
let reversedword =
word.ToCharArray()
|> Array.fold(fun acc x -> x::acc) []
ExpandDiskEdit
erlang
Reversed = lists:reverse("reverse me"),
ExpandDiskEdit
erlang
Reversed = revchars("reverse me"),
DiskEdit
ocaml
let reverse str =
let len = String.length str in
let res = String.create len in
for i = 0 to pred len do
let j = pred len - i in
res.[i] <- str.[j]
done;
(res)
ExpandDiskEdit
ocaml
let rev_char str =
let l = Str.split (Str.regexp "") str in
List.fold_left (fun a b -> b ^ a) "" l
;;
ExpandDiskEdit
csharp 3.0
var str = "reverse me";
Console.WriteLine(new String(str.Reverse().ToArray()));
ExpandDiskEdit
php
$reversed = strrev("reverse me");
DiskEdit
haskell
reverse "reverse me"
DiskEdit
clojure
(require '[clojure.contrib.str-utils2 :as str])
(str/reverse "reverse me")
DiskEdit
clojure
(apply str (reverse "reverse me"))
ExpandDiskEdit
fantom
"reverse me".reverse
DiskEdit
go
package main;
import "utf8";
import "fmt";

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)
}

func main() {
fmt.Print(reverse("reverse me"));
}
ExpandDiskEdit
go go1
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)
}

Submit a new solution for ruby, java, perl, groovy ...