Reverse a string: Difference between revisions

m
→‎{{header|Oberon}}: Fixed language name
m (→‎{{header|Oberon}}: Fixed language name)
 
(17 intermediate revisions by 12 users not shown)
Line 436:
<syntaxhighlight lang="applescript">{{"! ereht olleH", {5, 4, 3, 2, 1}},
{"! ereht olleH", {5, 4, 3, 2, 1}}}</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="applesoftbasic">10 A$ = "THE FIVE BOXING WIZARDS JUMP QUICKLY"
20 GOSUB 100REVERSE
30 PRINT R$
40 END
 
100 REMREVERSE A$
110 R$ = ""
120 FOR I = 1 TO LEN(A$)
130 R$ = MID$(A$, I, 1) + R$
140 NEXT I
150 RETURN</syntaxhighlight>
 
=={{header|Arturo}}==
Line 564 ⟶ 551:
 
=={{header|BASIC}}==
 
{{works with|QBasic|1.1}}
==={{header|Applesoft BASIC}}===
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasicapplesoftbasic">function10 reverseA$(a$) = "THE FIVE BOXING WIZARDS JUMP QUICKLY"
20 GOSUB 100REVERSE
b$ = ""
30 PRINT R$
for i = 1 to len(a$)
40 END
b$ = mid$(a$, i, 1) + b$
 
next i
100 REMREVERSE A$
reverse$ = b$
110 R$ = ""
end function</syntaxhighlight>
120 FOR I = 1 TO LEN(A$)
130 R$ = MID$(A$, I, 1) + R$
140 NEXT I
150 RETURN</syntaxhighlight>
 
==={{header|BASIC256}}===
Line 588 ⟶ 579:
{{out}}
<pre>'asdf' reversed is 'fdsa'</pre>
 
==={{header|Commodore BASIC}}===
{{works with|Commodore BASIC|3.5,7.0}}
Commodore BASIC 3.5 turned MID$ into an lvalue function, and assigning a string of the same length to MID$ replaces the characters instead of allocating a new string, so the reversal can be done in-place:
 
<syntaxhighlight lang="basic">
100 INPUT "STRING";S$
110 FOR I=1 TO INT(LEN(S$)/2)
120 : J=LEN(S$)+1-I
130 : T$=MID$(S$,I,1)
140 : MID$(S$,I,1) = MID$(S$,J,1)
150 : MID$(S$,J,1) = T$
160 NEXT I
170 PRINT S$</syntaxhighlight>
{{Out}}
<pre>STRING? THIS IS A TEST
TSET A SI SIHT
 
READY.</pre>
 
==={{header|IS-BASIC}}===
Line 596 ⟶ 606:
150 NEXT
160 PRINT REV$</syntaxhighlight>
 
==={{header|QuickBASIC}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">function reverse$(a$)
b$ = ""
for i = 1 to len(a$)
b$ = mid$(a$, i, 1) + b$
next i
reverse$ = b$
end function</syntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Line 727 ⟶ 748:
 
<syntaxhighlight lang="befunge">55+~>:48>*#8\#4`#:!#<#~_$>:#,_@</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
This 9 byte program, featured on https://www.ioccc.org/2012/tromp/hint.html, reverses its input in byte-oriented BLC:
 
<pre>16 46 80 17 3e f0 b7 b0 40</pre>
 
=={{header|BQN}}==
Line 1,358 ⟶ 1,385:
 
=={{header|Elm}}==
<syntaxhighlight lang="elm">--module TheMain import on the next line provides the reverse stringexposing (main)
-- functionality satisfying the rosettacode.org task description.
import String exposing (reverse)
 
import Html exposing (Html, text, div, p)
-- The rest is fairly boilerplate code demonstrating
import Html.Attributes exposing (style)
-- interactively that the reverse function works.
import Html exposing (Html, Attribute, text, div, input)
import Html.Attributes exposing (placeholder, value, style)
import Html.Events exposing (on, targetValue)
import Html.App exposing (beginnerProgram)
 
main = beginnerProgram { model = "", view = view, update = update }
 
change myText =
update newStr oldStr = newStr
text ("reverse " ++ myText
++ " = " ++ String.reverse myText)
 
view : String -> Html String
view forward =
div []
([ input
[ placeholder "Enter a string to be reversed."
, value forward
, on "input" targetValue
, myStyle
]
[]
] ++
[ let backward = reverse forward
in div [ myStyle] [text backward]
])
 
main =
myStyle : Attribute msg
div [style "margin" "5%", style "font-size" "1.5em"]
myStyle =
[change "as⃝da"
style
, p [] ("width",[change "100%a⃝su-as⃝u")]
, ("height",p [] [change "20pxHello!")]
]
, ("padding", "5px 0 0 5px")
</syntaxhighlight>
, ("font-size", "1em")
, ("text-align", "left")
]</syntaxhighlight>
 
Link to live demo: httphttps://dc25ellie-app.github.io/reverseStringElmcom/qdg6RP3DGCBa1
{{out}}
<pre>
reverse as⃝da = ad⃝sa
reverse a⃝su-as⃝u = u⃝sa-us⃝a
reverse Hello! = !olleH
</pre>
 
=={{header|Emacs Lisp}}==
Line 1,703 ⟶ 1,715:
<syntaxhighlight lang="clojure">
!dlrow olleH
</syntaxhighlight>
 
=={{header|Fennel}}==
Uses the same methods (and suffers from the same limitations) as [[#Lua|the Lua example]].
<syntaxhighlight lang="fennel">
(let [example :asdf]
(string.reverse example) ; fdsa
(example:reverse) ; fdsa
nil)
</syntaxhighlight>
 
Line 2,117 ⟶ 2,138:
=={{header|Io}}==
<syntaxhighlight lang="io">"asdf" reverse</syntaxhighlight>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(reverse "hello")
</syntaxhighlight>
 
=={{header|J}}==
Line 2,355 ⟶ 2,382:
 
=={{header|langur}}==
ThisThe accountsreverse() forfunction codewill points,reverse buta notstring foraccording to graphemes.
<syntaxhighlight lang="langur">writeln cp2s reverse s2cp q("don't you know)"</syntaxhighlight>
 
{{out}}
Line 2,999 ⟶ 3,026:
60 PRINT REVERSED$</syntaxhighlight>
 
=={{header|Oberon-2}}==
Tested with [https://miasap.se/obnc OBNC].
<syntaxhighlight lang="oberon">MODULE reverse;
Line 3,822 ⟶ 3,849:
<syntaxhighlight lang="red">>> reverse "asdf"
== "fdsa"</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Reverse 'asdf'>>;
};
 
Reverse {
(e.X) = e.X;
(e.X) s.C e.Y = <Reverse (s.C e.X) e.Y>;
e.X = <Reverse () e.X>;
};</syntaxhighlight>
{{out}}
<pre>fdsa</pre>
 
=={{header|ReScript}}==
Line 3,936 ⟶ 3,976:
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
→ str""
"" strOVER SIZE 1 '''FOR''' j str j DUP SUB + -1 '''STEP'''
OVER j DUP SUB +
≫ ≫ 'RVSTR' STO
-1 '''STEP''' SWAP DROP
≫ '<span style="color:blue">RVSTR</span>' STO
"ABC" <span style="color:blue">RVSTR</span>
{{out}}
<pre>1: "CBA"</pre>
Line 4,145 ⟶ 4,187:
 
=={{header|Seed7}}==
Seed7 strings are encoded with UTF-32 therefore no special Unicode solution is necessary. The following demonstrates one way of reversing a string with a user-defined function.
<syntaxhighlight lang="seed7ada" line="1">$ include "seed7_05.s7i";
 
const func string: reverse reverso(in string: stri) is func
result
var string: result is "";
Line 4,161 ⟶ 4,203:
const proc: main is func
begin
writeln(reversereverso("Was it a cat I saw"));
end func;</syntaxhighlight>The following demonstrates the use of the built-in 'reverse' function:<syntaxhighlight lang="ada" line="1">
$ include "seed7_05.s7i";
{{out}}
 
const proc: main is func
begin
writeln(reverse("Was it a cat I saw?"));
end func;
</syntaxhighlight>{{out}}
<pre>
was I tac a ti saW
Line 4,277 ⟶ 4,325:
. di ustrreverse(s)
νῆγ νὴτ ὶακ νὸναρὐο νὸτ ςὸεθ ὁ νεσηίοπἐ ῇχρἀ νἘ</syntaxhighlight>
 
=={{header|Stringle}}==
This inputs a string from the user and outputs its reverse. The <code>\</code> ''reverse'' operator reverses any string.
 
<syntaxhighlight lang="stringle">$ \$</syntaxhighlight>
 
=={{header|Swift}}==
Line 4,692 ⟶ 4,745:
{{libheader|Wren-str}}
{{libheader|Wren-upc}}
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
import "./upc" for Graphemes
 
for (word in ["asdf", "josé", "møøse", "was it a car or a cat I saw", "😀🚂🦊"]) {
3,038

edits