I've got this problem, I want to make some kind of programm, which could change all the words to uppercase in all combinations, like (input = something -> echo something; Something; sOmething; ... sOMeTHIng; sOMeTHiNg;... SOMETHING) Any ideas ?
Asked
Active
Viewed 874 times
0
-
Some extra considerations for getting you an accurate answer(specifically for letters appearing more than once): 1. Say the word is "test". In your situation, is "Test" a different combination than "TesT"? 2. If there are multiple words, like "a taste", is "A tAste" different than "A taste"? – panhandel Jun 06 '13 at 20:36
-
this is a kind of `permutation`, wait for [Aacini](http://stackoverflow.com/users/778560/aacini), he may help :) – Endoro Jun 06 '13 at 20:42
-
@Endoro: Thanks a lot! You have plenty confidence in me... :-) – Aacini Jun 07 '13 at 00:02
2 Answers
3
EDIT: I realized that my first solution does not correctly manage repeated permutations caused by non-alphabetic characters, like spaces. I modified the Batch file below to fix this detail.
@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

Aacini
- 65,180
- 12
- 72
- 108
-
-
@MichalHerobrineSeják: Well, if you think this answer solved your question, you may accept it as Best answer... :) – Aacini Jun 08 '13 at 02:12
1
This solution properly handles characters in string that do not have case.
@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
Assuming script is PERMUTE.BAT, here is a sample call with output:
>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

dbenham
- 127,446
- 28
- 251
- 390