Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)

by eyepopslikeamosquito (Archbishop)
on Dec 12, 2006 at 12:27 UTC ( [id://589260]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
     "  one   two three four    "
    
  2. or download this
     "four three two one"
    
  3. or download this
     private static string reverseWords(string str) {
         string[] words = Array.FindAll<string>(str.Split(
    ...
             sb.Append(' ').Append(words[i]);
         return sb.ToString();
     }
    
  4. or download this
     Array.Reverse(words);
     return String.Join(" ", words);
    
  5. or download this
     sub reverseWords {
         join ' ', reverse split(' ', shift)
     }
    
  6. or download this
     sub reverseWords(Str $s) returns Str {
         $s.words.reverse.join(' ')
     }
    
  7. or download this
     def reverseWords(s)
         s.split.reverse.join(' ')
     end
    
  8. or download this
     reverseWords = unwords . reverse . words
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://589260]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-18 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found