文字列の反転
Posted feedbacks - Batchfile
cmd.exeで。 文字列の長さをしる方法があれば、forが使えそうですが、 わかりませんでした。 >rev_str "Hello" olleH >rev_str "こんにちは" はちにんこ >rev_str "濁点(だくてん)" )んてくだ(点濁
1 2 3 4 5 6 7 8 9 10 | @echo off
setlocal
set a=%~1
set b=
:loop
set b=%a:~0,1%%b%
set a=%a:~1%
if not "%a%"=="" goto loop
echo %b%
endlocal
|
文字列長を求めてから文字列を反転する方法を考えてみました。文字列が半角空白を含ま
ない場合は、引数をダブルクォーテーションで括る必要はありません。
e.g.
C:\>reverse "Hello"
olleH
C:\>reverse "こんにちは"
はちにんこ
C:\>reverse "濁点(だくてん)"
)んてくだ(点濁
遅延環境変数展開を利用しているので、Windows NTでは動作しません。Windows 2000, XP,
2003で動作を確認。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | @echo off
setlocal
set t=%1
set t=%t:"=%
call :reverse "%t%" t
echo %t%
endlocal
goto :EOF
:reverse
setlocal enabledelayedexpansion
set l=0
set r=
set t=%1
set t=%t:"=%
call :length "%t%" l
for /l %%i in (1,1,%l%) do set r=!r!!t:~-%%i,1!
endlocal & set %2=%r%
goto :EOF
:length
setlocal
set i=0
set t=%1
set t=%t:"=%
:loop
set t=%t:~1%
set /a i+=1
if not "%t%" == "" goto loop
endlocal & set %2=%i%
goto :EOF
|






にしお
#3414()
Rating0/2=0.00
サンプル入出力
>>> print reverse_string("Hello") olleH >>> print reverse_string("こんにちは") はちにんこ >>> print reverse_string("濁点(だくてん)") )んてくだ(点濁[ reply ]