Saturday, November 24, 2012

Shell Script to reverse a String

Q: How do I reverse a string in Linux/Unix?
Ans:

#!/bin/bash
read -p "Enter string:" string
len=${#string}
for (( i=$len-1; i>=0; i-- ))
do
# "${string:$i:1}"extract single single character from string.
reverse="$reverse${string:$i:1}"
done
echo "$reverse"

Output


Enjoy :)

0 comments:

Post a Comment