Thursday, October 6, 2022

[SOLVED] reverse the order of characters in a string

Issue

In string "12345", out string "54321". Preferably without third party tools and regex.


id='dv4'>

Solution

Simple:

var="12345"
copy=${var}

len=${#copy}
for((i=$len-1;i>=0;i--)); do rev="$rev${copy:$i:1}"; done

echo "var: $var, rev: $rev"

Output:

$ bash rev
var: 12345, rev: 54321


Answered By - osdyng
Answer Checked By - Robin (WPSolving Admin)