Solve : Reversing a string in batch?

Microsoft Microsoft DOS in Microsoft 3 years ago

  1   0   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

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

Posted on 21 May 2022, this text provides information on Microsoft related to Microsoft DOS in Microsoft. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Tuteehub forum answer Answers

Post Answer

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.