How to resolve the algorithm Phrase reversals step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Phrase reversals step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Given a string of space separated words containing the following phrase:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Phrase reversals step by step in the UNIX Shell programming language

Source code in the unix programming language

s1="rosetta code phrase reversal"
echo "Original string ----------------------> "$s1

echo -n "1.) Reverse the string ---------------> "
echo $s1|rev

echo -n "2.) Reverse characters of each word --> "
echo $s1|tr " " "\n"|rev|tr "\n" " ";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


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


  

You may also check:How to resolve the algorithm Aliquot sequence classifications step by step in the C++ programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the Haskell programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the Nim programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the Haskell programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean/Calculate Pi step by step in the PicoLisp programming language