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