1.

Solve : Reversing a string in batch?

Answer»

As the title suggests, I want to reverse a string/variable in batch. Eg. the input is HELLO and the output is olleh. I know it is possible in vbs, but if possible, the script needs to be put in batch.Code: [Select]echo off
setlocal enabledelayedexpansion

set LINE=sdas sfsa fwfwa21321
set num=0

:LOOP
call set tmpa=%%line:~%num%,1%%%
set /a num+=1
if not "%tmpa%" equ "" (
set rline=%tmpa%%rline%
goto LOOP
)
echo %rline%

pauseThanks so much!anything can be put inside  a put batch, including the cscript /wscript engine. Its better next to time state you want "pure cmd.exe commands without use of external TOOLS".
As always Devcom's  effort is over and above.  Does Devcom work for Microsoft?

C:\>type  hi.bat
echo off
Code: [Select]setlocal enabledelayedexpansion

set line=%1
echo  line = %line%
set num=0

:LOOP
call set tmpa=%%line:~%num%,1%%%
set /a num+=1
if not "%tmpa%" equ "" (
set rline=%tmpa%%rline%
goto LOOP
)
echo reverse = %rline%
Output:

C:\> hi.bat  Hello
 line = Hello
reverse = olleH
C:\>

_______________________________________ _

For a laugh I will show a newbie effort with batch:


C:\>type  hello.bat
Code: [Select]echo off

REM  http://www.dostips.com/DtTipsStringManipulation.php

setlocal enabledelayedexpansion

set x=%1
echo x = %x%

set x=%x:~-2,-1%
echo  %x% > rev.txt
set x=%1
set x=%x:~-3,-2%
echo  %x% >> rev.txt
set x=%1
set x=%x:~-4,-3%
echo  %x% >> rev.txt
set x=%1
set x=%x:~-5,-4%
echo  %x% >> rev.txt
set x=%1
set x=%x:~-6,-5%
echo  %x% >> rev.txt
type rev.txt
C:\>hello.bat  "Hello"
x = "Hello"
 o
 L
 l
 e
 H

C:\> Quote from: billrich on June 20, 2009, 02:54:12 PM

As always Devcom's  effort is over and above.  Does Devcom work for Microsoft?

lol , im not here's a one liner, using gawk
Code: [Select]C:\test>echo hello| gawk "BEGIN{FS=\"\"}{for(i=NF;i>0;i--)printf $i}"
olleh


Discussion

No Comment Found