Phrase reversals: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 18:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V phrase = ‘rosetta code phrase reversal’
print(reversed(phrase))
print(phrase.split(‘ ’).map(word -> reversed(word)).join(‘ ’))
print(reversed(phrase.split(‘ ’)).join(‘ ’))</langsyntaxhighlight>
 
{{out}}
Line 31:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC ReversePart(CHAR ARRAY src,dst BYTE start,len)
BYTE i
 
Line 82:
ReverseWords(s,rev)
PrintE(rev)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Phrase_reversals.png Screenshot from Atari 8-bit computer]
Line 97:
[[http://rosettacode.org/wiki/Reverse_words_in_a_string#Ada]] is used.
 
<langsyntaxhighlight Adalang="ada"><with Ada.Text_IO, Simple_Parse;
 
procedure Phrase_Reversal is
Line 139:
Put_Line("2. Reverse words, same order: """ & Reverse_Words(Phrase) & """");
Put_Line("2. Reverse order, same words: """ & Reverse_Order(Phrase) & """");
end Phrase_Reversal;</langsyntaxhighlight>
 
{{out}}
Line 150:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># reverses the characters in str from start pos to end pos #
PROC in place reverse = ( REF STRING str, INT start pos, INT end pos )VOID:
BEGIN
Line 204:
, original phrase, ": order reversed -> ", order reversed, newline
)
)</langsyntaxhighlight>
{{out}}
<pre>
Line 220:
Here is a simple illustration of unifying (and elevating) the function type by wrapping the built-in functions in user handlers (perhaps making some of them polymorphic where needed), and also obtaining first class status for ordinary user handler functions by 'lifting' them (for use as arguments in higher order functions) into a first class script object. (This process can be inlined, or abstracted out to an '''mReturn''' or '''mInject''' function).
 
<langsyntaxhighlight AppleScriptlang="applescript">-- REVERSED PHRASES, COMPONENT WORDS, AND WORD ORDER ---------------------
 
-- reverseString, reverseEachWord, reverseWordOrder :: String -> String
Line 343:
on unwords(lstWords)
intercalate(space, lstWords)
end unwords</langsyntaxhighlight>
{{out}}
<pre>"lasrever esarhp edoc attesor
Line 352:
===Idiomatic===
 
<langsyntaxhighlight lang="applescript">set aString to "rosetta code phrase reversal"
 
set astid to AppleScript's text item delimiters
Line 369:
set AppleScript's text item delimiters to astid
 
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"lasrever esarhp edoc attesor
attesor edoc esarhp lasrever
reversal phrase code rosetta"</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">phr: "rosetta code phrase reversal"
 
print ["(0)" phr]
print ["(1)" reverse phr]
print ["(2)" join.with:" " map split.words phr => reverse]
print ["(3)" join.with:" " reverse split.words phr]</langsyntaxhighlight>
 
{{out}}
Line 393:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">var =
<lang AutoHotKey>var =
(
Rosetta Code Phrase Reversal
Line 426:
ExitApp
 
esc::ExitApp</langsyntaxhighlight>
 
{{out}}
Line 437:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk"># Usage: awk -f phrase_revers.awk
function rev(s, del, n,i,a,r) {
n = split(s, a, del)
Line 454:
printf( fmt, "word-order reversed", wr )
printf( fmt, "each word reversed", rev(wr) )
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 464:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="qbasic">phrase$ = "rosetta code phrase reversal"
 
PRINT REVERSE$(phrase$)
Line 470:
PRINT REV$(REVERSE$(phrase$))
 
PRINT REV$(phrase$)</langsyntaxhighlight>
{{out}}
<pre>
Line 479:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
%=== The Main Thing... ===%
Line 527:
set %var3%=!%var3%:~1,1000000!
goto :EOF
%=== /Reverse each Words Function ===%</langsyntaxhighlight>
{{Out}}
<pre>Original: Rosetta Code phrase reversal
Line 536:
=={{header|Bracmat}}==
This example only works correctly with strings only consisting of byte-sized characters.
<langsyntaxhighlight lang="bracmat">( "rosetta code phrase reversal":?text
& rev$!text:?output1
& get$(!text,MEM):?words
Line 551:
$ ("0:\"" !text "\"\n1:\"" !output1 "\"\n2:\"" !output2 "\"\n3:\"" !output3 \"\n)
)
);</langsyntaxhighlight>
Output:
<pre>0:"rosetta code phrase reversal"
Line 560:
=={{header|C}}==
Working with C strings is often long-winded.
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <string.h>
Line 631:
return 0;
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 642:
=={{header|C sharp}}==
 
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
namespace ConsoleApplication
Line 663:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <algorithm>
Line 686:
reverse_copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << '\n' ;
}</langsyntaxhighlight>
{{Out}}
<pre>Input : rosetta code phrase reversal
Line 695:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(use '[clojure.string :only (join split)])
(def phrase "rosetta code phrase reversal")
(defn str-reverse [s] (apply str (reverse s)))
Line 705:
; Word order reversed
(apply str (interpose " " (reverse (split phrase #" "))))
</syntaxhighlight>
</lang>
{{out}}
<pre>"lasrever esarhp edoc attesor"
Line 712:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
<lang COBOL>
program-id. phra-rev.
data division.
Line 748:
.
end program phra-rev.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 758:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun split-string (str)
"Split a string into space separated words including spaces"
Line 776:
nil )
 
</syntaxhighlight>
</lang>
{{out}}<pre>(task "rosetta code phrase reversal")
 
Line 787:
=={{header|D}}==
Partially lazy.
<langsyntaxhighlight lang="d">void main() @safe {
import std.stdio, std.range, std.algorithm;
 
Line 794:
phrase.splitter.map!retro.joiner(" ").writeln; // Words reversed.
phrase.split.retro.joiner(" ").writeln; // Word order reversed.
}</langsyntaxhighlight>
{{out}}
<pre>lasrever esarhp edoc attesor
Line 801:
 
=={{header|Dyalect}}==
<langsyntaxhighlight lang="dyalect">let str = "rosetta code phrase reversal"
//or you can use a built-in method String.reverse
Line 834:
print("1. \(reverse(str))")
print("2. \(reverseByWord(str))")
print("3. \(reverseWords(str))")</langsyntaxhighlight>
 
{{out}}
Line 843:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(define (string-reverse string)
(list->string (reverse (string->list string))))
Line 858:
"attesor edoc esarhp lasrever"
"reversal phrase code rosetta"
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
ELENA 4.x :
<langsyntaxhighlight lang="elena">import extensions;
import extensions'text;
import system'routines;
Line 879:
//Reverse the order of each word of the phrase, maintaining the order of characters in each word.
console.printLine(reverse(phrase.splitBy:" ".selectBy:(s => s + " ")))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 889:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">str = "rosetta code phrase reversal"
 
IO.puts String.reverse(str)
IO.puts String.split(str) |> Enum.map(&String.reverse(&1)) |> Enum.join(" ")
IO.puts String.split(str) |> Enum.reverse |> Enum.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 903:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(defun reverse-sep (words sep)
(mapconcat 'identity (reverse (split-string words sep)) sep))
 
Line 921:
(terpri)
(princ (reverse-words line))
(terpri))</langsyntaxhighlight>
 
{{out}}
Line 930:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">
USE: splitting
 
Line 940:
 
"rosetta code phrase reversal" [ reverse-string ] [ reverse-words ] [ reverse-phrase ] tri
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 953:
By identifying the first and last character position of each word in TEXT (or equivalently, their indices in ATXT) and storing them in arrays IST and LST, the i'th word can be fingered via IST(i) to LST(i) and of course a DO-loop can step in either direction.
 
F90 allows a WHILE-loop, and writing something like <langsyntaxhighlight Fortranlang="fortran"> DO WHILE (L1.LE.L .AND. ATXT(L1).LE." ")
L1 = L1 + 1
END DO</langsyntaxhighlight>
Would be rather more structured and involve fewer GO TOs and their labels, but alas, modern Fortran specifies that there is no specification as to whether or not both terms of an expression such as (A '''and''' B) will always be evaluated or instead there will be a shortcut: if A is ''false'' then the B term is ignored. And here, the A term checks whether or not L1 is within bounds and if it is not, then the B term should not be evaluated, not just because of the waste of effort but because to do so might involve accessing outside the definition of ATXT. As when the scan chases through the trailing spaces. One could make the array one longer, or rather, <code>L = LEN(TEXT) - 1</code> for this case but that would be messy, require explanation, be easily forgotten, and, typical ad-hoc testing would be unlikely to detect the mistake.
 
Alternatively, a GO TO can be removed from view by using EXIT in its place: <langsyntaxhighlight Fortranlang="fortran"> DO L1 = L1,L
IF (ATXT(L1).GT." ") EXIT
END DO</langsyntaxhighlight> Except that this relies on the index variable retaining its value on exiting the loop, either as fingering the first non-blank or, being L + 1. This expectation is frowned upon in some quarters.
 
Both variants would have to be followed by a test such as <code>IF (L1 .LE. L) THEN</code> to identify whether the start of a word has been found that would require further processing. So, all in all, suck up the GO TOs...<langsyntaxhighlight Fortranlang="fortran"> PROGRAM REVERSER !Just fooling around.
CHARACTER*(66) TEXT !Holds the text. Easily long enough.
CHARACTER*1 ATXT(66) !But this is what I play with.
Line 1,002:
 
22 FORMAT (A36,":",66A1)
END</langsyntaxhighlight>
With F77 such array spans can't be used, but all that is necessary is to supply a second implied DO-loop in the WRITE statement, for example <langsyntaxhighlight Fortranlang="fortran"> WRITE (6,22) RW//RO,(" ",(ATXT(J), J = LST(I),IST(I),-1), I = 1,N,+1)</langsyntaxhighlight>
 
And the output is...
Line 1,016:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub split (s As Const String, sepList As Const String, result() As String)
Line 1,079:
Print : Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,100:
Frink also has smart, human-language aware functions for splitting a string into a list of words. It correctly handles hyphenated words, numbers, and punctuation. It also has a built-in smart <CODE>reverseWords</CODE> function to reverse the words in a string.
 
<langsyntaxhighlight lang="frink">a = "rosetta code phrase reversal"
println[reverse[a]]
println[join["", map["reverse", wordList[a]]]]
Line 1,106:
// Alternately, the above could be
// join["", reverse[wordList[a]]]
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,117:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=77cb8b3386a0f57524bdbff6634387cd Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sString As String = "rosetta code phrase reversal" 'The string
Dim sNewString, sTemp As String 'String variables
Line 1,152:
Print "Words reversed = \t" & sNewString 'Print words reversed
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,163:
=={{header|Go}}==
===Simple===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,197:
}
fmt.Println("Word order reversed:", strings.Join(ws, " "))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,205:
</pre>
===Alternative===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,258:
fmt.Println("Words reversed: ", reverseWords(phrase))
fmt.Println("Word order reversed:", reverseWordOrder(phrase))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,267:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def phaseReverse = { text, closure -> closure(text.split(/ /)).join(' ')}
 
def text = 'rosetta code phrase reversal'
Line 1,273:
println "Reversed: ${phaseReverse(text) { it.reverse().collect { it.reverse() } } }"
println "Reversed Words: ${phaseReverse(text) { it.collect { it.reverse() } } }"
println "Reversed Order: ${phaseReverse(text) { it.reverse() } }"</langsyntaxhighlight>
{{out}}
<pre>Original: rosetta code phrase reversal
Line 1,281:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">reverseString, reverseEachWord, reverseWordOrder :: String -> String
reverseString = reverse
 
Line 1,295:
(putStrLn . unlines) $
[reverseString, reverseEachWord, reverseWordOrder] <*>
["rosetta code phrase reversal"]</langsyntaxhighlight>
{{Out}}
<pre>lasrever esarhp edoc attesor
Line 1,302:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "ReverseS.bas"
110 LET S$="Rosetta Code Pharse Reversal"
120 PRINT S$
Line 1,328:
340 NEXT
350 LET REVERSEC$=LTRIM$(T$)
360 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j"> getWords=: (' '&splitstring) :. (' '&joinstring)
reverseString=: |.
reverseWords=: |.&.>&.getWords
reverseWordOrder=: |.&.getWords</langsyntaxhighlight>
'''Usage:'''
<langsyntaxhighlight lang="j"> phrase=: 'rosetta code phrase reversal'
(reverseWordOrder , reverseWords ,: reverseString) phrase
reversal phrase code rosetta
attesor edoc esarhp lasrever
lasrever esarhp edoc attesor</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
 
public class PhraseRev{
Line 1,380:
System.out.println("Reversed word order: " + join(reverse(str.split(" ")), " "));
}
}</langsyntaxhighlight>
{{out}}
<pre>Straight-up reversed: lasrever esarhp edoc attesor
Line 1,388:
=={{header|JavaScript}}==
===ES5===
<langsyntaxhighlight JavaScriptlang="javascript">(function (p) {
return [
p.split('').reverse().join(''),
Line 1,400:
].join('\n');
 
})('rosetta code phrase reversal');</langsyntaxhighlight>
{{out}}
<pre>lasrever esarhp edoc attesor
Line 1,407:
 
===ES6===
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict'
 
Line 1,471:
], ["rosetta code phrase reversal"])
);
})();</langsyntaxhighlight>
{{Out}}
<pre>lasrever esarhp edoc attesor
Line 1,479:
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def reverse_string: explode | reverse | implode;
 
"rosetta code phrase reversal"
Line 1,486:
"1. string reversed: \(reverse_string)",
"2. each word reversed: \($words | map(reverse_string) | join(" "))",
"3. word-order reversed: \($words | reverse | join(" "))"</langsyntaxhighlight>
{{out}}
$ jq -r -n -f Phrase_reversals.jq
Line 1,495:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
s = "rosetta code phrase reversal"
 
Line 1,512:
t = join(reverse(split(s, " ")), " ")
println(" ", t)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,527:
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
/ Rosetta code phrase reversal
/ phraserev.k
Line 1,536:
revwordorder: {rw:""; while[~(x~""); x: getnxtwd x;rw:" ",rw;rw:w,rw];:-1 _ rw}
 
</syntaxhighlight>
</lang>
The output of a session is given below:
{{out}}
Line 1,555:
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">include ..\Utilitys.tlhy
"Rosetta Code Phrase Reversal" dup ?
Line 1,562:
len [drop pop swap reverse print " " print] for drop nl nl
 
"End " input</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun reverseEachWord(s: String) = s.split(" ").map { it.reversed() }.joinToString(" ")
Line 1,576:
println("Reversed words => ${reverseEachWord(original)}")
println("Reversed order => ${reverseEachWord(reversed)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,587:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
{W.reverse word} reverses characters in a word
{S.reverse words} reverses a sequence of words
Line 1,597:
{S.map W.reverse {S}} -> attesor edoc esarhp lasrever
{S.reverse {S}} -> reversal phrase code rosetta
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Return a copy of table t in which each string is reversed
function reverseEach (t)
local rev = {}
Line 1,626:
print("1. " .. str:reverse())
print("2. " .. table.concat(reverseEach(tab), " "))
print("3. " .. table.concat(tabReverse(tab), " "))</langsyntaxhighlight>
{{out}}
<pre>1. lasrever esarhp edoc attesor
Line 1,633:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">#reverse the string
str := "rosetta code phrase reversal":
print(StringTools:-Reverse(str)):
Line 1,643:
print(StringTools:-Join(convert(lst,list)," ")):
#reverse word order
print(StringTools:-Join(ListTools:-Reverse(StringTools:-Split(str," ")), " ")):</langsyntaxhighlight>
{{Out|Output}}
<pre> "lasrever esarhp edoc attesor"
Line 1,650:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">phrase = "Rosetta Code Phrase Reversal";
 
reverseWords[phrase_String] :=
Line 1,662:
 
{phrase, reverseWords@phrase, reverseLetters@phrase,
reverseWords@reverseLetters@phrase} // TableForm</langsyntaxhighlight>
 
{{out}}<pre>
Line 1,672:
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
<lang Matlab>
function r=revstr(s,d)
slist=strsplit(s,d);
Line 1,687:
 
revstr('Rosetta Code Phrase Reversal', ' ')
</syntaxhighlight>
</lang>
 
 
Line 1,700:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">phrase = "rosetta code phrase reversal"
 
// general sequence reversal function
Line 1,724:
// 3. Reverse the order of each word of the string, maintaining the order of characters in each word.
print reverse(phrase.split).join
</syntaxhighlight>
</lang>
{{out}}
<pre>lasrever esarhp edoc attesor
Line 1,731:
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">set string="Rosetta Code Phrase Reversal"
set str="",len=$length(string," ")
for i=1:1:len set $piece(str," ",i)=$piece(string," ",len-i+1)
Line 1,737:
write $reverse(string),!
write str,!
write $reverse(str),!</langsyntaxhighlight>
 
{{out}}<pre>Rosetta Code Phrase Reversal
Line 1,746:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils
 
const Phrase = "rosetta code phrase reversal"
Line 1,753:
echo "Reversed phrase: ", reversed(Phrase).join()
echo "Reversed words: ", Phrase.split().mapIt(reversed(it).join()).join(" ")
echo "Reversed word order: ", reversed(Phrase.split()).join(" ")</langsyntaxhighlight>
 
If we prefer to avoid using modules “algorithm” and “sequtils” and produce somewhat more efficient (but also more verbose) code, here is another solution:
 
<langsyntaxhighlight Nimlang="nim">import strutils
 
const Phrase = "rosetta code phrase reversal"
Line 1,780:
echo "Reversed phrase: ", reversed(Phrase)
echo "Reversed words: ", reversedWords(Phrase)
echo "Reversed word order: ", reversedWordOrder(Phrase)</langsyntaxhighlight>
 
{{out}}
Line 1,790:
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">"rosetta code phrase reversal" reverse println
"rosetta code phrase reversal" words map(#reverse) unwords println
"rosetta code phrase reversal" words reverse unwords println</langsyntaxhighlight>
 
{{out}}
Line 1,803:
=={{header|Perl}}==
 
<langsyntaxhighlight lang="perl">use feature 'say';
my $s = "rosetta code phrase reversal";
 
Line 1,812:
 
# Or, using a regex:
say "2. Each word reversed : ", $s =~ s/[^ ]+/reverse $&/gre;</langsyntaxhighlight>
 
{{out}}
Line 1,824:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">test</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"rosetta code phrase reversal"</span><span style="color: #0000FF;">,</span>
Line 1,837:
<span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">)),</span>
<span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,847:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
"Rosetta Code Phrase Reversal" dup print nl
dup reverse print nl
split dup reverse len for . pop swap print " " print endfor . nl
len for . pop swap reverse print " " print endfor .</langsyntaxhighlight>
{{out}}
<pre>Rosetta Code Phrase Reversal
Line 1,860:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
// Initialize a variable with the input desired
$strin = "rosetta code phrase reversal";
Line 1,888:
// Show the reversal of the word order while leaving the words in order
echo "Word order reversed: ".$str_word_order_reversed."\n";
</syntaxhighlight>
</lang>
 
<pre>Input: rosetta code phrase reversal
Line 1,896:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">import util.
 
go =>
Line 1,904:
println([reverse(W) : W in S.split()].join(' ')),
println(reverse(S.split()).join(' ')),
nl.</langsyntaxhighlight>
 
{{out}}
Line 1,914:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let (S (chop "rosetta code phrase reversal") L (split S " "))
(prinl (reverse S))
(prinl (glue " " (mapcar reverse L)))
(prinl (glue " " (reverse L))) )</langsyntaxhighlight>
Output:
<pre>lasrever esarhp edoc attesor
Line 1,924:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
reverser: procedure options (main); /* 19 August 2015 */
declare (phrase, r, word) character (100) varying;
Line 1,946:
 
end reverser;
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,956:
 
=={{header|plainTeX}}==
<langsyntaxhighlight lang="tex">\def\afterfi#1#2\fi{#2\fi#1}
\def\RevSingleWord#1{\RevSingleWordi{}#1\RSWA\RSWB\RSWB\RSWB\RSWB\RSWB\RSWB\RSWB\RSWB\RSWA}
\def\RevSingleWordi#1#2#3#4#5#6#7#8#9{\RSWgobtoB#9\RSWend\RSWB\RevSingleWordi{#9#8#7#6#5#4#3#2#1}}
Line 1,995:
Reverse order, same words&\RevOrderSameWords{rosetta code phrase reversal}\cr
Reverse only words&\RevOnlyWords{rosetta code phrase reversal}\cr\crcr}
\bye</langsyntaxhighlight>
 
pdf or dvi output looks like:
Line 2,005:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function reverse($a, $sep = "") {
if($a.Length -gt 0) {
Line 2,019:
$task2
$task3
</langsyntaxhighlight>
<b>Output:</b>
<pre>
Line 2,028:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#TEXT="rosetta code phrase reversal"
 
If OpenConsole("rosetta code phrase reversal")
Line 2,058:
Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Original: rosetta code phrase reversal
Line 2,067:
=={{header|Python}}==
These examples use the [https://docs.python.org/2/whatsnew/2.3.html#extended-slices extended slicing] notation of <code>[::-1]</code> to reverse strings and lists of strings:
<langsyntaxhighlight lang="python">>>> phrase = "rosetta code phrase reversal"
>>> phrase[::-1] # Reversed.
'lasrever esarhp edoc attesor'
Line 2,074:
>>> ' '.join(phrase.split()[::-1]) # Word order reversed.
'reversal phrase code rosetta'
>>> </langsyntaxhighlight>
 
 
Or, variously composing three reusable abstractions – '''reverse''', '''words''', and '''unwords''':
 
<langsyntaxhighlight lang="python">'''String reversals at different levels.'''
 
 
Line 2,167:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>rosetta code phrase reversal:
Line 2,177:
=={{header|Quackery}}==
 
<langsyntaxhighlight lang="quackery"> $ "rosetta code phrase reversal"
 
3 times dup
Line 2,184:
say "1. " reverse echo$ cr
say "2. " nest$ witheach [ reverse echo$ sp ] cr
say "3. " nest$ reverse witheach [ echo$ sp ] cr</langsyntaxhighlight>
 
{{out}}
Line 2,195:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require
(only-in srfi/13 string-reverse)
Line 2,206:
(string-join (reverse (string-split s)))))
 
(for-each displayln (phrase-reversal "rosetta code phrase reversal"))</langsyntaxhighlight>
 
{{out}}
Line 2,215:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my $s = 'rosetta code phrase reversal';
 
put 'Input : ', $s;
put 'String reversed : ', $s.flip;
put 'Each word reversed : ', $s.words».flip;
put 'Word-order reversed : ', $s.words.reverse;</langsyntaxhighlight>
{{out}}
<pre>Input : rosetta code phrase reversal
Line 2,230:
===version 1===
Working with REXX (strings and words) is trivial.
<langsyntaxhighlight lang="rexx">s='rosetta code phrase reversal'
r1=reverse(s)
r2=''
Line 2,245:
say "string reversed : " r1
say "each word reversed : " r2
say "word-order reversed : " r3</langsyntaxhighlight>
{{out}}
<pre>input : rosetta code phrase reversal
Line 2,253:
 
===version 2===
<langsyntaxhighlight lang="rexx">/*REXX program reverses words and also letters in a string in various (several) ways. */
parse arg $ /*obtain optional arguments from the CL*/
if $='' then $= "rosetta code phrase reversal" /*Not specified? Then use the default.*/
Line 2,263:
say ' original phrase reversed: ' reverse($)
say ' reversed individual words: ' strip(L)
say ' reversed words in phrases: ' W /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input string:}}
<pre>
Line 2,273:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
aString = "Welcome to the Ring Language"
bString = ""
Line 2,283:
next
return bString
</syntaxhighlight>
</lang>
 
Output:
Line 2,291:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">str = "rosetta code phrase reversal"
 
puts str.reverse # Reversed string.
puts str.split.map(&:reverse).join(" ") # Words reversed.
puts str.split.reverse.join(" ") # Word order reversed.</langsyntaxhighlight>
 
{{out}}
Line 2,305:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn reverse_string(string: &str) -> String {
string.chars().rev().collect::<String>()
}
Line 2,356:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object PhraseReversals extends App {
val phrase = scala.io.StdIn.readLine
println(phrase.reverse)
println(phrase.split(' ').map(_.reverse).mkString(" "))
println(phrase.split(' ').reverse.mkString(" "))
}</langsyntaxhighlight>
 
{{out}}
Line 2,374:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,393:
end for;
writeln("Reverse order, same words:" rpad 27 <& join(wordList, ' '));
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,404:
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">set phrase to "rosetta code phrase reversal"
put phrase reversed
put (the reverse of each word of phrase) joined by space
put (each word of phrase) reversed joined by space
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,417:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var str = "rosetta code phrase reversal";
 
say str.reverse; # reversed string
say str.words.map{.reverse}.join(' '); # words reversed
say str.words.reverse.join(' '); # word order reversed</langsyntaxhighlight>
{{out}}
<pre>lasrever esarhp edoc attesor
Line 2,429:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltlak">|str|
str := 'rosetta code phrase reversal'.
 
Transcript showCR:(str reversed).
Transcript showCR:(((str splitBy:$ ) collect:#reversed) join:$ ).
Transcript showCR:(((str splitBy:$ ) reversed) join:$ ).</langsyntaxhighlight>
{{out}}
<pre>lasrever esarhp edoc attesor
Line 2,441:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">
func reverseString(s:String)->String{
var temp = [Character]()
Line 2,480:
print(reverseWord(s:str))
print(flipString(s:str))
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set s "rosetta code phrase reversal"
# Reverse all characters
puts [string reverse $s]
Line 2,489:
puts [lmap word $s {string reverse $word}]
# Reverse the words but not the characters
puts [lreverse $s]</langsyntaxhighlight>
{{out}}
<pre>
Line 2,503:
{{works with|bash}}
{{works with|ksh93}}
<langsyntaxhighlight lang="sh">s1="rosetta code phrase reversal"
echo "Original string ----------------------> "$s1
 
Line 2,516:
while [ $word_num != 0 ];do
echo -n $(echo $s1|cut -d " " -f $word_num);echo -n " "
word_num=$(expr $word_num - 1);done;echo</langsyntaxhighlight>
 
{{out}}
Line 2,530:
{{works with|bash}}
{{works with|ksh93}}
<langsyntaxhighlight lang="sh">s1="rosetta code phrase reversal"
echo "Original string --> "$s1
 
Line 2,555:
while [ $word_num != 0 ];do
echo -n $(echo $s1|cut -d " " -f $word_num);echo -n " "
word_num=$(expr $word_num - 1);done;echo</langsyntaxhighlight>
 
{{out}}
Line 2,565:
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 2,598:
Reverse_the_order_of_each_word = Trim(strTemp)
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>Original String : rosetta code phrase reversal
Line 2,606:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Phrase = "rosetta code phrase reversal"
 
Line 2,645:
Next
End Function
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,655:
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">
fn main() {
str := 'rosetta code phrase reversal'
Line 2,663:
println('Char-Word Reverse: ${words.map(it.reverse()).join(' ')}')
println('Word Reverse: ${words.reverse().join(' ')}')
}</langsyntaxhighlight>
{{out}}
<pre>Original: rosetta code phrase reversal
Line 2,671:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var s = "rosetta code phrase reversal"
System.print("Input : %(s)")
System.print("String reversed : %(s[-1..0])")
System.print("Each word reversed : %(s.split(" ").map { |w| w[-1..0] }.join(" "))")
System.print("Word order reversed : %(s.split(" ")[-1..0].join(" "))")</langsyntaxhighlight>
 
{{out}}
Line 2,687:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">phrase$ = "Rosetta Code Phrase Reversal"
 
dim word$(1)
Line 2,721:
return rw$
end sub</langsyntaxhighlight>
{{out}}
<pre>Rosetta Code Phrase Reversal
Line 2,729:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">zkl: var str="rosetta code phrase reversal"
rosetta code phrase reversal
 
Line 2,739:
 
zkl: str.split().reverse().concat(" ") #3
reversal phrase code rosetta</langsyntaxhighlight>
10,333

edits