将输入中的所有小写字母逐渐更改为大写以获得批处理中的所有组合

时间:2013-06-06 20:21:26

标签: batch-file uppercase letters

我遇到了这个问题,我想制作某种程序,可以将所有单词改为大写,例如(input = something - > echo something; Something; sOmething; .. .SOMeTHIng; sOMeTHiNg; ... SOMETHING) 有任何想法吗 ?

2 个答案:

答案 0 :(得分:3)

编辑:我意识到我的第一个解决方案无法正确管理由非字母字符(如空格)引起的重复排列。我修改了下面的批处理文件以修复此细节。

@echo off
setlocal EnableDelayedExpansion

set /P "input=Enter input: "

rem Convert "input" into "upcase" and "lowcase", and create "letters" string
set upcase=%input%
set lowcase=%input%
set letters=
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M"
            "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do (
   set upcase=!upcase:%%~a!
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      set lowcase=!lowcase:%%c=%%b!
      set letters=!letters!%%c
   )
)

rem Separate individual characters in "char" two dimensional array, and create "bit" array
rem Non-alphabetic characters are marked with "bit" elements = 2
set n=-1
:nextChar
   set /A n+=1
   set char=%lowcase:~0,1%
   if "!letters:%char%=!" neq "%letters%" (
      set bit[%n%]=0
      set char[%n%,0]=%char%
      set char[%n%,1]=%upcase:~0,1%
   ) else (
      set bit[%n%]=2
      set char[%n%,2]=%char%
   )
   set upcase=%upcase:~1%
   set lowcase=%lowcase:~1%
if defined lowcase goto nextChar

rem Generate all permutations
:nextValue
   rem Copy characters from "char" array into "output" string with current "bit" values
   set output=
   for /L %%i in (0,1,%n%) do (
      for %%b in (!bit[%%i]!) do (
         set output=!output!!char[%%i,%%b]!
      )
   )
   rem Show this permutation
   echo %output%
   rem Advance "bit" array to next value
   set /A b=n+1, carry=1
   :nextBit
      set /A b-=1
      if !bit[%b%]! lss 2 (
         if !bit[%b%]! equ 0 (
            set /A bit[%b%]=1, carry=0
         ) else (
            set /A bit[%b%]=0, carry=1
         )
      )
   if %b% gtr 0 if %carry% equ 1 goto nextBit
if %carry% equ 0 goto nextValue

答案 1 :(得分:1)

此解决方案正确处理没有大小写的字符串中的字符。

@echo off
setlocal enableDelayedExpansion

set "string=%~1"
set "permutation="
set pos=0

for %%C in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do set "lower_%%C=%%C"
for %%C in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do set "upper_%%C=%%C"

if defined string call :permute
exit /b

:permute
setlocal
set "chr=!string:~%pos%,1!"
if not defined chr (
  echo(!permutation!
  exit /b
)
set /a pos+=1
for /f delims^=^ eol^= %%C in ("!chr!") do (
  if defined upper_%%C (
    set "permutation=!permutation!!lower_%%C!"
    call :permute
    set "permutation=!permutation:~0,-1!!upper_%%C!"
    call :permute
  ) else (
    set "permutation=!permutation!%%C"
    call :permute
  )
)
exit /b

假设脚本是PERMUTE.BAT,这是一个带输出的示例调用:

>permute.bat "test it"
test it
test iT
test It
test IT
tesT it
tesT iT
tesT It
tesT IT
teSt it
teSt iT
teSt It
teSt IT
teST it
teST iT
teST It
teST IT
tEst it
tEst iT
tEst It
tEst IT
tEsT it
tEsT iT
tEsT It
tEsT IT
tESt it
tESt iT
tESt It
tESt IT
tEST it
tEST iT
tEST It
tEST IT
Test it
Test iT
Test It
Test IT
TesT it
TesT iT
TesT It
TesT IT
TeSt it
TeSt iT
TeSt It
TeSt IT
TeST it
TeST iT
TeST It
TeST IT
TEst it
TEst iT
TEst It
TEst IT
TEsT it
TEsT iT
TEsT It
TEsT IT
TESt it
TESt iT
TESt It
TESt IT
TEST it
TEST iT
TEST It
TEST IT