I'm trying to reverse "hello" to "olleh". But the output shows "ooooo".
I think !string:~%back%,1!
is the problem, because when I use echo
to test the value of back
is decreasing or not, it works, but it doesn't work in substring, so it always get the last character of the string (-1,1).
@echo off
set string=hello
set temp_string=%string%
set /a string_length=0
:find_length
if defined temp_string (
set temp_string=%temp_string:~1%
set /a string_length+=1
goto :find_length
)
:loop
setlocal enabledelayedexpansion
set /a back=-1
for /l %%a in (1,1,!string_length!) do (
set reverse_string=!string:~%back%,1!!reverse_string!
set /a back-=1
)
echo !reverse_string!
pause >nul