2.倒序输出字符串

2.倒序输出字符串

#!/bin/bash
while :
do
read -p "输入一个字符串:" str
if [ "$str" = "" ];
then
echo "error"
exit
fi

len=${#str}
res=
for ((i = $len;i>=0;i--))
do
res=$res${str:$i:1}# 利用字符切片从后往前一次取出一个字符并连接
done
echo "$res"
done

猜你喜欢

转载自blog.csdn.net/hhsx15/article/details/83181867