in reply to Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
The J language isn't particularly strong in string manipulation. You can get an easy solution this way:
but it sort of feels like cheating because of the use of the ;: verb which breaks a string to J tokens.reverseWords =: (,' '&,)&:>/@:|.@:;: reverseWords ' one two three four ' four three two one
A solution without this cheating is (I have replaced the tab character with a hat for legibility):
Another solution is:reverseWord1 =: [:(,' '&,)&:>/@:|.@:-.&a:e.&' ^'<@}.;.1] reverseWord1 ' one two three^four ' four three two one
reverseWord2 =: [:}.@;@|.e.&' ^'<@(' '&,@}.`(0$])@.(2>#));.1] reverseWord2 ' one two three^four ' four three two one
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
by ambrus (Abbot) on Feb 13, 2009 at 20:21 UTC |
In Section
Meditations