Reply to comment

Batch tricks: Reversing text

If there is one thing the Windows Command Processor (cmd.exe) can do (except starting other programs) it's string processing. Not at Perl's level but certainly more sohpisticated than the C standard library (not counting regex here).

I was just playing around a bit and came up with this:

@echo off
setlocal enableextensions enabledelayedexpansion
set "DATA=%*"
:loop
set REVDATA=%REVDATA%%DATA:~-1%
set DATA=%DATA:~0,-1%
if defined DATA goto loop
echo %REVDATA%
endlocal

Pretty straightforward, as usual. The obligatory setlocal with the usual options (I almost always set them, regardless whether I need them or not). Saving all command line arguments into a string and then dissecting it character by character. As soon as the original string is eaten up we can quit and output the result.

And it works in Unicode, too:

T:\>reverse ↔¾Ω∞()‡‼ αβγδ да ◙
◙ ад δγβα ‼‡)(∞Ω¾↔

AttachmentSize
Reverse190 bytes

Reply

The content of this field is kept private and will not be shown publicly.