View Problem

Reverse the words in a string

Given the string "This is a end, my only friend!", produce the string "friend! only my end, the is This"
ExpandDiskEdit
ruby
reversed = text.split.reverse.join(' ')
ExpandDiskEdit
java
List list = new ArrayList();
StringTokenizer st = new StringTokenizer(text, " ");
while(st.hasMoreTokens()) {
list.add(0, st.nextToken());
}
StringBuffer sb = new StringBuffer();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String word = (String) iterator.next();
sb.append(word);
if (iterator.hasNext()) {
sb.append(" ");
}
}
String reversed = sb.toString();
ExpandDiskEdit
java 1.5 or later
List<String> ls = Arrays.asList("This is the end, my only friend!".split("\\s"));
Collections.reverse(ls);
StringBuilder sb = new StringBuilder(32); for (String s : ls) sb.append(" ").append(s);
String reversed = sb.toString().trim();
ExpandDiskEdit
java org.apache.commons
String reversed = StringUtils.reverseDelimited("This is the end, my only friend!", ' ');
DiskEdit
perl
$reversed = join ' ', reverse split / /, $text;
ExpandDiskEdit
groovy
reversed = "This is the end, my only friend!".split().reverse().join(' ')
ExpandDiskEdit
groovy
reversed = "This is the end, my only friend!".tokenize(' ').reverse().join(' ')
ExpandDiskEdit
groovy apache commons lang
def revdelim(c, s) { StringUtils.reverseDelimited(s, c) }
revwords = this.&revdelim.curry(" " as char)
reversed = revwords("This is the end, my only friend!")
ExpandDiskEdit
groovy apache commons lang
reversed = StringUtils.reverseDelimited("This is the end, my only friend!", " " as char)
DiskEdit
scala
"This is the end, my only friend!".split(" ").reverse.reduceLeft( (x,y) => x+' '+y )
ExpandDiskEdit
scala org.apache.commons
val reversed = revwords("This is the end, my only friend!")
ExpandDiskEdit
scala
(("This is the end, my only friend!" split " ") reverse) mkString " "
ExpandDiskEdit
scala
val reversedText = text.split(" ").reverse.mkString(" ")
DiskEdit
python
' '.join(reversed("This is a end, my only friend!".split()))
ExpandDiskEdit
cpp C++/CLI .NET 2.0
array<Char>^ sep = {L' '};
array<String^>^ words =
String(L"This is the end, my only friend!").Split(sep, StringSplitOptions::RemoveEmptyEntries);

Array::Reverse(words); String^ newwords = String::Join(L" ", words);
ExpandDiskEdit
cpp
std::string words = "This is the end, my only friend!"; std::vector<std::string> swv;

boost::split(swv, words, boost::is_any_of(" ")); std::reverse(swv.begin(), swv.end());
std::string newwords = (std::for_each(swv.begin(), swv.end(), StringTAndJ())).value();
ExpandDiskEdit
fsharp
let reversed = String.Join(" ", Array.rev("This is the end, my only friend!".Split [|' '|]))
ExpandDiskEdit
erlang
Reversed = string:join(lists:reverse(string:tokens("This is the end, my only friend!", " ")), " "),
ExpandDiskEdit
ocaml
let rev_words str =
let l = Str.split (Str.regexp " ") str in
String.concat " " (List.rev l)
;;
ExpandDiskEdit
csharp 3.0
var str = "This is a end, my only friend!";
str = String.Join(" ", str.Split().Reverse().ToArray());
Console.WriteLine(str);
ExpandDiskEdit
php
$reversed_words = implode(" ", array_reverse(explode(" ", "This is the end, my only friend!")));
DiskEdit
haskell
unwords (reverse (words "This is the end, my only friend!"))
DiskEdit
clojure
(require '[clojure.contrib.str-utils2 :as str])
(str/join " " (reverse (str/split "this is the end, my only friend!" #" ")))
DiskEdit
clojure
(apply str (interpose " " (reverse (re-seq #"[^\s]+" "This is the end, my only friend!"))))
ExpandDiskEdit
fantom
"This is a end, my only friend!".split.reverse.join(" ")
ExpandDiskEdit
go
words := strings.Split("This is the end, my only friend!", " ")
nr := len(words)
reversed := make([]string, nr)
for i, word := range words {
reversed[nr - i - 1] = word
}
s := strings.Join(reversed, " ")
fmt.Println(s)
ExpandDiskEdit
go
func reverse(list []string) ([]string) {
if len(list) == 1 {
return list
}

return append(reverse(list[1:]), list[0])
}

func main() {
words := strings.Split("This is the end, my only friend!", " ")
s := strings.Join(reverse(words), " ")
fmt.Println(s)
}

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