Algorithms, Blockchain and Cloud

Another Batch Utility: Reverse the Given Text


If you have something that are not so important but prefer not so easy to read at first except to you. You can simply reverse the text. For example, reversing the text ‘Hello, World!’ becomes, ‘!dlroW ,olleH’.

To write a helpful utility is easy. Scripting languages are the best tools of making such tools. Simply read in the string and output the string in the reverse order. Windows batch programming can handle such easy jobs without any help of external programs (the users won’t need to download other external programs in order to make this work). The reason of choosing the batch script is that it is supported on every versions of Windows.

The following script first determines the length [see this post also] of the command line script parameters using %*, then a for /l loop is used to retrieve each character from left to right and append it to the final result from right to left (reverse order).

@echo off
:: reverse.bat
:: dr@steakovercooked.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

The source script is also available at [github]. The improvement can be found [here].

–EOF (The Ultimate Computing & Technology Blog) —

346 words
Last Post: Make Lots of Directories with SMD.bat using Windows Batch
Next Post: Rot13 and Rot5 implementation using Windows Batch

The Permanent URL is: Another Batch Utility: Reverse the Given Text (AMP Version)

Exit mobile version