Phrase reversals: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 18:
{{trans|Python}}
<
print(reversed(phrase))
print(phrase.split(‘ ’).map(word -> reversed(word)).join(‘ ’))
print(reversed(phrase.split(‘ ’)).join(‘ ’))</
{{out}}
Line 31:
=={{header|Action!}}==
<
BYTE i
Line 82:
ReverseWords(s,rev)
PrintE(rev)
RETURN</
{{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.
<
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;</
{{out}}
Line 150:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<
PROC in place reverse = ( REF STRING str, INT start pos, INT end pos )VOID:
BEGIN
Line 204:
, original phrase, ": order reversed -> ", order reversed, newline
)
)</
{{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).
<
-- reverseString, reverseEachWord, reverseWordOrder :: String -> String
Line 343:
on unwords(lstWords)
intercalate(space, lstWords)
end unwords</
{{out}}
<pre>"lasrever esarhp edoc attesor
Line 352:
===Idiomatic===
<
set astid to AppleScript's text item delimiters
Line 369:
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
attesor edoc esarhp lasrever
reversal phrase code rosetta"</
=={{header|Arturo}}==
<
print ["(0)" phr]
print ["(1)" reverse phr]
print ["(2)" join.with:" " map split.words phr => reverse]
print ["(3)" join.with:" " reverse split.words phr]</
{{out}}
Line 393:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">var =
(
Rosetta Code Phrase Reversal
Line 426:
ExitApp
esc::ExitApp</
{{out}}
Line 437:
=={{header|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) )
}</
{{Out}}
<pre>
Line 464:
=={{header|BaCon}}==
<
PRINT REVERSE$(phrase$)
Line 470:
PRINT REV$(REVERSE$(phrase$))
PRINT REV$(phrase$)</
{{out}}
<pre>
Line 479:
=={{header|Batch File}}==
<
setlocal enabledelayedexpansion
%=== The Main Thing... ===%
Line 527:
set %var3%=!%var3%:~1,1000000!
goto :EOF
%=== /Reverse each Words Function ===%</
{{Out}}
<pre>Original: Rosetta Code phrase reversal
Line 536:
=={{header|Bracmat}}==
This example only works correctly with strings only consisting of byte-sized characters.
<
& rev$!text:?output1
& get$(!text,MEM):?words
Line 551:
$ ("0:\"" !text "\"\n1:\"" !output1 "\"\n2:\"" !output2 "\"\n3:\"" !output3 \"\n)
)
);</
Output:
<pre>0:"rosetta code phrase reversal"
Line 560:
=={{header|C}}==
Working with C strings is often long-winded.
<syntaxhighlight lang="c">
#include <stdio.h>
#include <string.h>
Line 631:
return 0;
}
</syntaxhighlight>
{{Out}}
<pre>
Line 642:
=={{header|C sharp}}==
<
using System.Linq;
namespace ConsoleApplication
Line 663:
}
}
}</
=={{header|C++}}==
<
#include <vector>
#include <algorithm>
Line 686:
reverse_copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << '\n' ;
}</
{{Out}}
<pre>Input : rosetta code phrase reversal
Line 695:
=={{header|Clojure}}==
<
(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>
{{out}}
<pre>"lasrever esarhp edoc attesor"
Line 712:
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
program-id. phra-rev.
data division.
Line 748:
.
end program phra-rev.
</syntaxhighlight>
{{out}}
<pre>
Line 758:
=={{header|Common Lisp}}==
<
(defun split-string (str)
"Split a string into space separated words including spaces"
Line 776:
nil )
</syntaxhighlight>
{{out}}<pre>(task "rosetta code phrase reversal")
Line 787:
=={{header|D}}==
Partially lazy.
<
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.
}</
{{out}}
<pre>lasrever esarhp edoc attesor
Line 801:
=={{header|Dyalect}}==
<
//or you can use a built-in method String.reverse
Line 834:
print("1. \(reverse(str))")
print("2. \(reverseByWord(str))")
print("3. \(reverseWords(str))")</
{{out}}
Line 843:
=={{header|EchoLisp}}==
<
(define (string-reverse string)
(list->string (reverse (string->list string))))
Line 858:
"attesor edoc esarhp lasrever"
"reversal phrase code rosetta"
</syntaxhighlight>
=={{header|Elena}}==
ELENA 4.x :
<
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 + " ")))
}</
{{out}}
<pre>
Line 889:
=={{header|Elixir}}==
<
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(" ")</
{{out}}
Line 903:
=={{header|Emacs Lisp}}==
<
(mapconcat 'identity (reverse (split-string words sep)) sep))
Line 921:
(terpri)
(princ (reverse-words line))
(terpri))</
{{out}}
Line 930:
=={{header|Factor}}==
<
USE: splitting
Line 940:
"rosetta code phrase reversal" [ reverse-string ] [ reverse-words ] [ reverse-phrase ] tri
</syntaxhighlight>
{{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 <
L1 = L1 + 1
END DO</
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: <
IF (ATXT(L1).GT." ") EXIT
END DO</
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...<
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</
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 <
And the output is...
Line 1,016:
=={{header|FreeBASIC}}==
<
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</
{{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.
<
println[reverse[a]]
println[join["", map["reverse", wordList[a]]]]
Line 1,106:
// Alternately, the above could be
// join["", reverse[wordList[a]]]
</syntaxhighlight>
{{out}}
Line 1,117:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=77cb8b3386a0f57524bdbff6634387cd Click this link to run this code]'''
<
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</
Output:
<pre>
Line 1,163:
=={{header|Go}}==
===Simple===
<
import (
Line 1,197:
}
fmt.Println("Word order reversed:", strings.Join(ws, " "))
}</
{{out}}
<pre>
Line 1,205:
</pre>
===Alternative===
<
import (
Line 1,258:
fmt.Println("Words reversed: ", reverseWords(phrase))
fmt.Println("Word order reversed:", reverseWordOrder(phrase))
}</
{{out}}
<pre>
Line 1,267:
=={{header|Groovy}}==
<
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() } }"</
{{out}}
<pre>Original: rosetta code phrase reversal
Line 1,281:
=={{header|Haskell}}==
<
reverseString = reverse
Line 1,295:
(putStrLn . unlines) $
[reverseString, reverseEachWord, reverseWordOrder] <*>
["rosetta code phrase reversal"]</
{{Out}}
<pre>lasrever esarhp edoc attesor
Line 1,302:
=={{header|IS-BASIC}}==
<
110 LET S$="Rosetta Code Pharse Reversal"
120 PRINT S$
Line 1,328:
340 NEXT
350 LET REVERSEC$=LTRIM$(T$)
360 END DEF</
=={{header|J}}==
'''Solution:'''
<
reverseString=: |.
reverseWords=: |.&.>&.getWords
reverseWordOrder=: |.&.getWords</
'''Usage:'''
<
(reverseWordOrder , reverseWords ,: reverseString) phrase
reversal phrase code rosetta
attesor edoc esarhp lasrever
lasrever esarhp edoc attesor</
=={{header|Java}}==
{{works with|Java|1.5+}}
<
public class PhraseRev{
Line 1,380:
System.out.println("Reversed word order: " + join(reverse(str.split(" ")), " "));
}
}</
{{out}}
<pre>Straight-up reversed: lasrever esarhp edoc attesor
Line 1,388:
=={{header|JavaScript}}==
===ES5===
<
return [
p.split('').reverse().join(''),
Line 1,400:
].join('\n');
})('rosetta code phrase reversal');</
{{out}}
<pre>lasrever esarhp edoc attesor
Line 1,407:
===ES6===
<
'use strict'
Line 1,471:
], ["rosetta code phrase reversal"])
);
})();</
{{Out}}
<pre>lasrever esarhp edoc attesor
Line 1,479:
=={{header|jq}}==
{{works with|jq|1.4}}
<
"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(" "))"</
{{out}}
$ jq -r -n -f Phrase_reversals.jq
Line 1,495:
=={{header|Julia}}==
<syntaxhighlight lang="julia">
s = "rosetta code phrase reversal"
Line 1,512:
t = join(reverse(split(s, " ")), " ")
println(" ", t)
</syntaxhighlight>
{{out}}
Line 1,527:
=={{header|K}}==
<syntaxhighlight 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>
The output of a session is given below:
{{out}}
Line 1,555:
=={{header|Klingphix}}==
<
"Rosetta Code Phrase Reversal" dup ?
Line 1,562:
len [drop pop swap reverse print " " print] for drop nl nl
"End " input</
=={{header|Kotlin}}==
<
fun reverseEachWord(s: String) = s.split(" ").map { it.reversed() }.joinToString(" ")
Line 1,576:
println("Reversed words => ${reverseEachWord(original)}")
println("Reversed order => ${reverseEachWord(reversed)}")
}</
{{out}}
Line 1,587:
=={{header|Lambdatalk}}==
<syntaxhighlight 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>
=={{header|Lua}}==
<
function reverseEach (t)
local rev = {}
Line 1,626:
print("1. " .. str:reverse())
print("2. " .. table.concat(reverseEach(tab), " "))
print("3. " .. table.concat(tabReverse(tab), " "))</
{{out}}
<pre>1. lasrever esarhp edoc attesor
Line 1,633:
=={{header|Maple}}==
<
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," ")), " ")):</
{{Out|Output}}
<pre> "lasrever esarhp edoc attesor"
Line 1,650:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
reverseWords[phrase_String] :=
Line 1,662:
{phrase, reverseWords@phrase, reverseLetters@phrase,
reverseWords@reverseLetters@phrase} // TableForm</
{{out}}<pre>
Line 1,672:
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
function r=revstr(s,d)
slist=strsplit(s,d);
Line 1,687:
revstr('Rosetta Code Phrase Reversal', ' ')
</syntaxhighlight>
Line 1,700:
=={{header|MiniScript}}==
<
// 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>
{{out}}
<pre>lasrever esarhp edoc attesor
Line 1,731:
=={{header|MUMPS}}==
<
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),!</
{{out}}<pre>Rosetta Code Phrase Reversal
Line 1,746:
=={{header|Nim}}==
<
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(" ")</
If we prefer to avoid using modules “algorithm” and “sequtils” and produce somewhat more efficient (but also more verbose) code, here is another solution:
<
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)</
{{out}}
Line 1,790:
=={{header|Oforth}}==
<
"rosetta code phrase reversal" words map(#reverse) unwords println
"rosetta code phrase reversal" words reverse unwords println</
{{out}}
Line 1,803:
=={{header|Perl}}==
<
my $s = "rosetta code phrase reversal";
Line 1,812:
# Or, using a regex:
say "2. Each word reversed : ", $s =~ s/[^ ]+/reverse $&/gre;</
{{out}}
Line 1,824:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,847:
=={{header|Phixmonti}}==
<
"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 .</
{{out}}
<pre>Rosetta Code Phrase Reversal
Line 1,860:
=={{header|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>
<pre>Input: rosetta code phrase reversal
Line 1,896:
=={{header|Picat}}==
<
go =>
Line 1,904:
println([reverse(W) : W in S.split()].join(' ')),
println(reverse(S.split()).join(' ')),
nl.</
{{out}}
Line 1,914:
=={{header|PicoLisp}}==
<
(prinl (reverse S))
(prinl (glue " " (mapcar reverse L)))
(prinl (glue " " (reverse L))) )</
Output:
<pre>lasrever esarhp edoc attesor
Line 1,924:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
reverser: procedure options (main); /* 19 August 2015 */
declare (phrase, r, word) character (100) varying;
Line 1,946:
end reverser;
</syntaxhighlight>
Output:
<pre>
Line 1,956:
=={{header|plainTeX}}==
<
\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</
pdf or dvi output looks like:
Line 2,005:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function reverse($a, $sep = "") {
if($a.Length -gt 0) {
Line 2,019:
$task2
$task3
</
<b>Output:</b>
<pre>
Line 2,028:
=={{header|PureBasic}}==
<
If OpenConsole("rosetta code phrase reversal")
Line 2,058:
Input()
EndIf</
{{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:
<
>>> phrase[::-1] # Reversed.
'lasrever esarhp edoc attesor'
Line 2,074:
>>> ' '.join(phrase.split()[::-1]) # Word order reversed.
'reversal phrase code rosetta'
>>> </
Or, variously composing three reusable abstractions – '''reverse''', '''words''', and '''unwords''':
<
Line 2,167:
if __name__ == '__main__':
main()</
{{Out}}
<pre>rosetta code phrase reversal:
Line 2,177:
=={{header|Quackery}}==
<
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</
{{out}}
Line 2,195:
=={{header|Racket}}==
<
(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"))</
{{out}}
Line 2,215:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
put 'Input : ', $s;
put 'String reversed : ', $s.flip;
put 'Each word reversed : ', $s.words».flip;
put 'Word-order reversed : ', $s.words.reverse;</
{{out}}
<pre>Input : rosetta code phrase reversal
Line 2,230:
===version 1===
Working with REXX (strings and words) is trivial.
<
r1=reverse(s)
r2=''
Line 2,245:
say "string reversed : " r1
say "each word reversed : " r2
say "word-order reversed : " r3</
{{out}}
<pre>input : rosetta code phrase reversal
Line 2,253:
===version 2===
<
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. */</
{{out|output|text= when using the default input string:}}
<pre>
Line 2,273:
=={{header|Ring}}==
<
aString = "Welcome to the Ring Language"
bString = ""
Line 2,283:
next
return bString
</syntaxhighlight>
Output:
Line 2,291:
=={{header|Ruby}}==
<
puts str.reverse # Reversed string.
puts str.split.map(&:reverse).join(" ") # Words reversed.
puts str.split.reverse.join(" ") # Word order reversed.</
{{out}}
Line 2,305:
=={{header|Rust}}==
<
string.chars().rev().collect::<String>()
}
Line 2,356:
}
}
</syntaxhighlight>
=={{header|Scala}}==
<
val phrase = scala.io.StdIn.readLine
println(phrase.reverse)
println(phrase.split(' ').map(_.reverse).mkString(" "))
println(phrase.split(' ').reverse.mkString(" "))
}</
{{out}}
Line 2,374:
=={{header|Seed7}}==
<
const proc: main is func
Line 2,393:
end for;
writeln("Reverse order, same words:" rpad 27 <& join(wordList, ' '));
end func;</
{{out}}
Line 2,404:
=={{header|SenseTalk}}==
<
put phrase reversed
put (the reverse of each word of phrase) joined by space
put (each word of phrase) reversed joined by space
</syntaxhighlight>
{{out}}
<pre>
Line 2,417:
=={{header|Sidef}}==
<
say str.reverse; # reversed string
say str.words.map{.reverse}.join(' '); # words reversed
say str.words.reverse.join(' '); # word order reversed</
{{out}}
<pre>lasrever esarhp edoc attesor
Line 2,429:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<
str := 'rosetta code phrase reversal'.
Transcript showCR:(str reversed).
Transcript showCR:(((str splitBy:$ ) collect:#reversed) join:$ ).
Transcript showCR:(((str splitBy:$ ) reversed) join:$ ).</
{{out}}
<pre>lasrever esarhp edoc attesor
Line 2,441:
=={{header|Swift}}==
<
func reverseString(s:String)->String{
var temp = [Character]()
Line 2,480:
print(reverseWord(s:str))
print(flipString(s:str))
</syntaxhighlight>
=={{header|Tcl}}==
<
# 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]</
{{out}}
<pre>
Line 2,503:
{{works with|bash}}
{{works with|ksh93}}
<
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</
{{out}}
Line 2,530:
{{works with|bash}}
{{works with|ksh93}}
<
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</
{{out}}
Line 2,565:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 2,598:
Reverse_the_order_of_each_word = Trim(strTemp)
End Function
</syntaxhighlight>
{{out}}
<pre>Original String : rosetta code phrase reversal
Line 2,606:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Phrase = "rosetta code phrase reversal"
Line 2,645:
Next
End Function
</syntaxhighlight>
{{Out}}
<pre>
Line 2,655:
=={{header|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(' ')}')
}</
{{out}}
<pre>Original: rosetta code phrase reversal
Line 2,671:
=={{header|Wren}}==
<
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(" "))")</
{{out}}
Line 2,687:
=={{header|Yabasic}}==
<
dim word$(1)
Line 2,721:
return rw$
end sub</
{{out}}
<pre>Rosetta Code Phrase Reversal
Line 2,729:
=={{header|zkl}}==
<
rosetta code phrase reversal
Line 2,739:
zkl: str.split().reverse().concat(" ") #3
reversal phrase code rosetta</
|