1

我有一个名为“code”的字符串和一个名为“lenght”的int,它是“code”的长度,也是一个int“todo”,其值为0,1或2。

0 应该删除“长度”位置的字符。

1 应该将位置“lenght”的字符更改为随机字符。

2 应该在“长度”位置添加一个随机字符。

我如何归档这个目标?

SET code="%*f0"
set length=0

:loop0
if defined # (
    set #=%#:~1%
    set /A length += 1
    goto loop0
)

SET /A location=%RANDOM% * %lenght% / 32768 + 1
SET /A todo=%RANDOM% * 2 / 32768 + 1

IF %todo% == 0(

)
IF %todo% == 1(

)
IF %todo% == 2(

)
4

1 回答 1

0

如果正确理解你,那么:

@echo off
  setlocal enabledelayedexpansion
    set "str=%~f0"
    set "len=0"
    set "buf=%str%"

    :loop
    if defined buf (set buf=%buf:~1%& set /a "len+=1" & goto:loop)

    echo Delete char at %%len%% position^:
    set /a "len-=1"
    echo !str:~0,%len%!
    echo.
    echo Change char at %%len%% position for random^:
    rem set chars string
    set "map=abcdefghijklmnopqrstuvwxyz"
    rem convert it to array
    set "i=0"
    for /l %%i in (0, 1, 25) do (
      for /l %%j in (1, 1, 1) do (
        set "arr.!i!=!map:~%%i,%%j!" & set /a "i+=1"
      )
    )
    rem random set 0..25
    set /a "rnd=0+25*%random%/32768"
    echo !str:~0,%len%!!arr.%rnd%!
  endlocal
exit /b
于 2013-11-07T14:56:38.127 回答