[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Unix Shell programming

how can i get the reverse of a string which is stored in a variable given by the user in unix shell programming?
0
drunkenlogic
Asked:
drunkenlogic
  • 2
3 Solutions
 
mugman21Commented:
Drunkenlogic,

Use the Rev command, there is an example at http://www.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/Q_10109991.html

Mugman
0
 
vippxCommented:
Hi there.. try this solution
works !! :)

#!/bin/bash

WORD=$1

let LENGTH=${#WORD}-1

while [ "${n:=0}" -le "$LENGTH" ]
      do
            let pos="$LENGTH"-n      
            REVERSE="$REVERSE${WORD:pos:1}"
            let n+=1
      done

echo $REVERSE

Simple array manipulation

0
 
pratikroyCommented:
A simple korn shell :

rev.sh
-------
#!/bin/ksh
ZVSTRING="$1"
ZRLENGTH=`printf $ZVSTRING | wc -c | awk '{print $1 }'`
YRSTART=$ZRLENGTH
while [ $YRSTART -gt 0 ]
do
    YVCHR=`echo $ZVSTRING | cut -c$YRSTART`
    YOUTSTR="${YOUTSTR}${YVCHR}"
    (( YRSTART = YRSTART - 1 ))
done
print $YOUTSTR

To use :
smpd9:/oq/home1/n43125/junk>rev abcdefgh
hgfedcba
smpd9:/oq/home1/n43125/junk>

Hope this would give you an idea!
0
 
pratikroyCommented:
Or you can use it as :

smpd9:/oq/home1/n43125/junk>abc="abcdefgh"
smpd9:/oq/home1/n43125/junk>rstr=`rev $abc`
smpd9:/oq/home1/n43125/junk>echo $rstr
hgfedcba
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now