Phrase reversals: Difference between revisions
Content added Content deleted
(added UNIX Shell) |
(Added a second version to UNIX Shell) |
||
Line 626: | Line 626: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
===Version 1=== |
|||
Requires "rev" command. |
|||
{{works with|Almquist Shell}} |
{{works with|Almquist Shell}} |
||
{{works with|bash}} |
{{works with|bash}} |
||
Line 631: | Line 633: | ||
<lang sh>s1="rosetta code phrase reversal" |
<lang sh>s1="rosetta code phrase reversal" |
||
echo "Original string ----------------------> "$s1 |
echo "Original string ----------------------> "$s1 |
||
echo -n "1.) Reverse the string ---------------> " |
echo -n "1.) Reverse the string ---------------> " |
||
echo $s1|rev |
echo $s1|rev |
||
echo -n "2.) Reverse characters of each word --> " |
echo -n "2.) Reverse characters of each word --> " |
||
echo $s1|tr " " "\n"|rev|tr "\n" " ";echo |
echo $s1|tr " " "\n"|rev|tr "\n" " ";echo |
||
echo -n "3.) Reverse word order ---------------> " |
echo -n "3.) Reverse word order ---------------> " |
||
word_num=$(echo $s1|wc -w) |
word_num=$(echo $s1|wc -w) |
||
Line 647: | Line 652: | ||
2.) Reverse characters of each word --> attesor edoc esarhp lasrever |
2.) Reverse characters of each word --> attesor edoc esarhp lasrever |
||
3.) Reverse word order ---------------> reversal phrase code rosetta</pre> |
3.) Reverse word order ---------------> reversal phrase code rosetta</pre> |
||
===Version 2=== |
|||
Does not require "rev" command. |
|||
{{works with|Almquist Shell}} |
|||
{{works with|bash}} |
|||
{{works with|ksh93}} |
|||
<lang sh>s1="rosetta code phrase reversal" |
|||
echo "Original string --> "$s1 |
|||
echo -n "1.) Reverse the string --> " |
|||
length=$(echo $s1|wc -c) |
|||
while [ $length != 0 ];do |
|||
echo $s1|cut -c$length|tr -d "\n" |
|||
length=$(expr $length - 1) |
|||
done;echo |
|||
echo -n "2.) Reverse characters of each word --> " |
|||
word_quantity=$(echo $s1|wc -w) |
|||
word_quantity=$(expr $word_quantity + 1) |
|||
word_num=1 |
|||
while [ $word_num != $word_quantity ];do |
|||
length=$(echo $s1|cut -d " " -f $word_num|wc -c) |
|||
while [ $length != 0 ];do |
|||
echo $s1|cut -d " " -f $word_num|cut -c$length|tr -d "\n" |
|||
length=$(expr $length - 1);done;echo -n " " |
|||
word_num=$(expr $word_num + 1);done;echo |
|||
echo -n "3.) Reverse word order --> " |
|||
word_num=$(echo $s1|wc -w) |
|||
while [ $word_num != 0 ];do |
|||
echo -n $(echo $s1|cut -d " " -f $word_num);echo -n " " |
|||
word_num=$(expr $word_num - 1);done;echo</lang> |
|||
{{out}} |
|||
<pre>Original string --> rosetta code phrase reversal |
|||
1.) Reverse the string --> lasrever esarhp edoc attesor |
|||
2.) Reverse characters of each word --> attesor edoc esarhp lasrever |
|||
3.) Reverse word order --> reversal phrase code rosetta</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |