How can we reverse a string using Bourne Shell scripting?

Questions by Nehashri

Showing Answers 1 - 3 of 3 Answers

angel421

  • Sep 9th, 2011
 

[~/temp]$ cat deepak.sh
#! /bin/sh

# reverse a string

[[ -z "$@" ]] && STR=" a b c d e f g h i j k l m n o p q r s t u v w x y z " || STR="$@"

len=${#STR}

REV=""
for (( i=$len ; i>0 ; i-- ))
do
REV=$REV""${STR:$i-1:$i}
STR=${STR%${STR:$i-1:$i}}
done

echo "Reversed string"
echo $REV


Code:
[~/temp]$ ./deepak.sh
Reversed string
z y x w v u t s r q p o n m l k j i h g f e d c b a

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions