GeekInterview.com
Series: Subject: Topic:
Question: 20 of 195

how can we reverse a string using Bourne Shell scripting?

Asked by: Nehashri | Member Since Jan-2007 | Asked on: Feb 2nd, 2007

View all questions by Nehashri

Showing Answers 1 - 1 of 1 Answers
angel421

Answered On : Sep 9th, 2011

View all answers by angel421

[~/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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

C Interview Questions

C++ Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.