Reverse words in a string: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 366: | Line 366: | ||
----------------------- Robert Frost</pre> |
----------------------- Robert Frost</pre> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 539: | Line 538: | ||
----------------------- Robert Frost</pre> |
----------------------- Robert Frost</pre> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Line 636: | Line 634: | ||
}</lang> |
}</lang> |
||
Output is the same as everyone else's. |
Output is the same as everyone else's. |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
public class ReverseWordsInString |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
string text = @" |
|||
---------- Ice and Fire ------------ |
|||
fire, in end will world the say Some |
|||
ice. in say Some |
|||
desire of tasted I've what From |
|||
fire. favor who those with hold I |
|||
... elided paragraph last ... |
|||
Frost Robert ----------------------- |
|||
"; |
|||
foreach (string line in text.Split(Environment.NewLine)) { |
|||
//Splits on any whitespace, not just spaces |
|||
string[] words = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries); |
|||
Array.Reverse(words); |
|||
WriteLine(string.Join(" ", words)); |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 744: | Line 771: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
public class ReverseWordsInString |
|||
{ |
|||
public static void Main(string[] args) |
|||
{ |
|||
string text = @" |
|||
---------- Ice and Fire ------------ |
|||
fire, in end will world the say Some |
|||
ice. in say Some |
|||
desire of tasted I've what From |
|||
fire. favor who those with hold I |
|||
... elided paragraph last ... |
|||
Frost Robert ----------------------- |
|||
"; |
|||
foreach (string line in text.Split(Environment.NewLine)) { |
|||
//Splits on any whitespace, not just spaces |
|||
string[] words = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries); |
|||
Array.Reverse(words); |
|||
WriteLine(string.Join(" ", words)); |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 1,302: | Line 1,300: | ||
println[join[" ", reverse[split[%r/\s+/, line]]]] |
println[join[" ", reverse[split[%r/\s+/, line]]]] |
||
</lang> |
</lang> |
||
=={{header|Gema}}== |
|||
<lang gema>\L<G> <U>=@{$2} $1</lang> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
Line 1,357: | Line 1,352: | ||
----------------------- Robert Frost |
----------------------- Robert Frost |
||
</pre> |
</pre> |
||
=={{header|Gema}}== |
|||
<lang gema>\L<G> <U>=@{$2} $1</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,830: | Line 1,828: | ||
end repeat |
end repeat |
||
put txtrev</lang> |
put txtrev</lang> |
||
=={{header|LiveScript}}== |
=={{header|LiveScript}}== |
||
Line 1,960: | Line 1,957: | ||
----------------------- Robert Frost</pre> |
----------------------- Robert Frost</pre> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Line 2,476: | Line 2,472: | ||
Frost Robert ----------------------- |
Frost Robert ----------------------- |
||
</lang> |
</lang> |
||
=={{header|Perl 6}}== |
|||
We'll read input from stdin |
|||
<lang perl6>say ~.words.reverse for lines</lang> |
|||
{{out}} |
|||
<pre>------------ Fire and Ice ---------- |
|||
Some say the world will end in fire, |
|||
Some say in ice. |
|||
From what I've tasted of desire |
|||
I hold with those who favor fire. |
|||
... last paragraph elided ... |
|||
----------------------- Robert Frost</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,523: | Line 2,504: | ||
----------------------- Robert Frost |
----------------------- Robert Frost |
||
</pre> |
</pre> |
||
=={{header|PHP}}== |
|||
<lang php> |
|||
<?php |
|||
function strInv ($string) { |
|||
$str_inv = '' ; |
|||
for ($i=0,$s=count($string);$i<$s;$i++){ |
|||
$str_inv .= implode(' ',array_reverse(explode(' ',$string[$i]))); |
|||
$str_inv .= '<br>'; |
|||
} |
|||
return $str_inv; |
|||
} |
|||
$string[] = "---------- Ice and Fire ------------"; |
|||
$string[] = ""; |
|||
$string[] = "fire, in end will world the say Some"; |
|||
$string[] = "ice. in say Some"; |
|||
$string[] = "desire of tasted I've what From"; |
|||
$string[] = "fire. favor who those with hold I"; |
|||
$string[] = ""; |
|||
$string[] = "... elided paragraph last ..."; |
|||
$string[] = ""; |
|||
$string[] = "Frost Robert ----------------------- "; |
|||
echo strInv($string);</lang> |
|||
'''Output''': |
|||
<lang Shell>------------ Fire and Ice ---------- |
|||
Some say the world will end in fire, |
|||
Some say in ice. |
|||
From what I've tasted of desire |
|||
I hold with those who favor fire. |
|||
... last paragraph elided ... |
|||
----------------------- Robert Frost</lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 2,532: | Line 2,556: | ||
{{Out}} |
{{Out}} |
||
Same as anybody else. |
Same as anybody else. |
||
=={{header|Pike}}== |
|||
<lang Pike>string story = #"---------- Ice and Fire ------------ |
|||
fire, in end will world the say Some |
|||
ice. in say Some |
|||
desire of tasted I've what From |
|||
fire. favor who those with hold I |
|||
... elided paragraph last ... |
|||
Frost Robert -----------------------"; |
|||
foreach(story/"\n", string line) |
|||
write("%s\n", reverse(line/" ")*" "); |
|||
</lang> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
Line 2,658: | Line 2,698: | ||
Frost Robert ----------------------- ---> ----------------------- Robert Frost |
Frost Robert ----------------------- ---> ----------------------- Robert Frost |
||
</pre> |
</pre> |
||
=={{header|PHP}}== |
|||
<lang php> |
|||
<?php |
|||
function strInv ($string) { |
|||
$str_inv = '' ; |
|||
for ($i=0,$s=count($string);$i<$s;$i++){ |
|||
$str_inv .= implode(' ',array_reverse(explode(' ',$string[$i]))); |
|||
$str_inv .= '<br>'; |
|||
} |
|||
return $str_inv; |
|||
} |
|||
$string[] = "---------- Ice and Fire ------------"; |
|||
$string[] = ""; |
|||
$string[] = "fire, in end will world the say Some"; |
|||
$string[] = "ice. in say Some"; |
|||
$string[] = "desire of tasted I've what From"; |
|||
$string[] = "fire. favor who those with hold I"; |
|||
$string[] = ""; |
|||
$string[] = "... elided paragraph last ..."; |
|||
$string[] = ""; |
|||
$string[] = "Frost Robert ----------------------- "; |
|||
echo strInv($string);</lang> |
|||
'''Output''': |
|||
<lang Shell>------------ Fire and Ice ---------- |
|||
Some say the world will end in fire, |
|||
Some say in ice. |
|||
From what I've tasted of desire |
|||
I hold with those who favor fire. |
|||
... last paragraph elided ... |
|||
----------------------- Robert Frost</lang> |
|||
=={{header|Pike}}== |
|||
<lang Pike>string story = #"---------- Ice and Fire ------------ |
|||
fire, in end will world the say Some |
|||
ice. in say Some |
|||
desire of tasted I've what From |
|||
fire. favor who those with hold I |
|||
... elided paragraph last ... |
|||
Frost Robert -----------------------"; |
|||
foreach(story/"\n", string line) |
|||
write("%s\n", reverse(line/" ")*" "); |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 2,825: | Line 2,806: | ||
(loop (read-line poem-port)))))) |
(loop (read-line poem-port)))))) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
We'll read input from stdin |
|||
<lang perl6>say ~.words.reverse for lines</lang> |
|||
{{out}} |
|||
<pre>------------ Fire and Ice ---------- |
|||
Some say the world will end in fire, |
|||
Some say in ice. |
|||
From what I've tasted of desire |
|||
I hold with those who favor fire. |
|||
... last paragraph elided ... |
|||
----------------------- Robert Frost</pre> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
<lang Red>Red [] |
<lang Red>Red [] |
||
Line 2,842: | Line 2,840: | ||
] |
] |
||
</lang> |
</lang> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===natural order=== |
===natural order=== |
||
Line 2,953: | Line 2,952: | ||
Output the same as everyone else's. |
Output the same as everyone else's. |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
Line 3,014: | Line 3,012: | ||
.join("\n")); // Concatenate lines into String |
.join("\n")); // Concatenate lines into String |
||
}</lang> |
}</lang> |
||
=={{header|S-lang}}== |
=={{header|S-lang}}== |