Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Reversing a string in batch  (Read 13805 times)

0 Members and 1 Guest are viewing this topic.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
Reversing a string in batch
« on: June 20, 2009, 10:59:20 AM »
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.
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

devcom



    Apprentice

    Thanked: 37
    Re: Reversing a string in batch
    « Reply #1 on: June 20, 2009, 12:59:35 PM »
    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%

    pause
    Download: Choice.exe

    Helpmeh

      Topic Starter


      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Reversing a string in batch
    « Reply #2 on: June 20, 2009, 01:06:59 PM »
    Thanks so much!
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Reversing a string in batch
      « Reply #3 on: June 20, 2009, 02:03:50 PM »
      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".

      billrich

      • Guest
      Re: Reversing a string in batch
      « Reply #4 on: June 20, 2009, 02:54:12 PM »
      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:\>

      devcom



        Apprentice

        Thanked: 37
        Re: Reversing a string in batch
        « Reply #5 on: June 20, 2009, 04:15:43 PM »
        As always Devcom's  effort is over and above.  Does Devcom work for Microsoft?

        lol , im not ;D
        Download: Choice.exe

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Reversing a string in batch
          « Reply #6 on: June 20, 2009, 09:34:56 PM »
          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