About 20 years ago, whoever did this one made a beaut. This

March 19, 2020 at 11:38:17
Specs: Windows 10
About 20 years ago, whoever did this one made a beaut. This batch file reverses a string, and pretty long ones.

@echo off
:: reverse.bat
:: dr@zhihua-lai.com
:: https://helloacm.com

setlocal
if [%1] neq [] goto start

echo Reverse Given Text
echo Usage: %0 text

goto :end

:start
set _len=0
set _str=%*

:: Get the length of the sentence
set _subs=%_str%

:loop
if not defined _subs goto :result

::remove the first char
set _subs=%_subs:~1%
set /a _len+=1
goto loop

:result
set /a _len-=1
for /l %%g in (0,1,%_len%) do (
call :build %%g
)

echo %s%

goto :end

:build
:: get the next character
call set _digit=%%_str:~%1,1%%%
set s=%_digit%%s%

:end
endlocal


See More: About 20 years ago, whoever did this one made a beaut. This


#1
March 23, 2020 at 02:31:52
is this a challange?

@echo off&setlocal enabledelayedexpansion
set "str=%~1"
echo string:%~1
call :strlen ans str
echo lenght:!ans!
for /l %%a in (1,1,!ans!) do (
	set "rv=!rv!!str:~-1!"
	set "str=!str:~0,-1!"
)
echo reverse:!rv!
pause>nul
exit/b
:strlen
set l=0
set "s=!%~2!#"
for %%P in (512 256 128 64 32 16 8 4 2 1) do (
	if "!s:~%%P,1!" NEQ "" (
		set/a "l+=%%P"
		set "s=!s:~%%P!"
	)
)
set "%~1=%l%"
exit /b

specs: https://www.userbenchmark.com/UserR...

message edited by hidde663


Report •

#2
March 25, 2020 at 13:12:21
I wrote this a few years ago.
It reverses each line in a text file.

:: script starts here ================
@echo off > NEWFILE.txt & setLocal enableDELAYedeXpansioN

if not exist myfile.txt if '%1' equ '' echo which file?  && goto :eof
if not exist myfile.txt copy %1 myfile.txt

:main
for /f "tokens=* delims= " %%a in (myfile.txt) do (
set S=%%a
call :sub0
echo.!N!
) >> NEWFILE.txt
goto :eof

:sub0
set N=
set L=

for %%C in (!S!) do (
:loop
set L=!S:~-1!
set S=!S:~0,-1!
  if "!S!" neq "" (
    set N=!N!!L!
    goto :loop
  ) else (
    set N=!N!!L!
  )
)
goto :eof
:: script ends here =========================

=====================

M2 Get custom script or take private lessons


Report •
Related Solutions


Ask Question