Reverse words in a string: Difference between revisions

no edit summary
No edit summary
Line 1,949:
Frost Robert ----------------------- ---> ----------------------- Robert Frost
</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|Python}}==
Anonymous user