Page 1 of 1

PermuateCase

Posted: Tue Apr 16, 2013 10:04 am
by Shield
I quickly needed such a functionality for some personal calculations,
so I might as well share it (even though it's probably of no real use to anyone,
it still shows a nice way to solve this problem I think). :)

Code: Select all

; //
; ////
; //// PermutateCase
; ////     Generate each possible version of uppercase and lowercase letter combination
; ////     For a word. Words containing characters that cannot be converted between cases
; ////     will produce duplicate results!
; ////
; //// Author:
; ////     Shield
; ////
; //


EnableExplicit


#MAX_COMBINATION_LENGTH = 16


Structure CharArray
	c.c[0]
EndStructure

Macro GetBit(__value, __bit)
	((__value) & (1 << (__bit)))
EndMacro


Procedure.i PermutateCase(word.s)
	Protected length.i
	Protected maskLength.i
	Protected maximum.i
	Protected counter.i
	Protected *wordIndex.CharArray
	Protected i.i
	
	
	If word = ""
		ProcedureReturn #False
	EndIf
	
	length = Len(word)
	If length > #MAX_COMBINATION_LENGTH
		ProcedureReturn #False
	EndIf
	
	*wordIndex = @word
	maskLength = length - 1
	maximum = 2 << maskLength
	counter = 0
	While counter < maximum
		For i = 0 To maskLength
			If GetBit(counter, i) = 0
				*wordIndex\c[i] = Asc(LCase(Chr(*wordIndex\c[i])))
			Else
				*wordIndex\c[i] = Asc(UCase(Chr(*wordIndex\c[i])))
			EndIf
		Next
		counter + 1
		PrintN(word)
	Wend
	
	ProcedureReturn #True
EndProcedure



OpenConsole()
; PermutateCase("test")
PermutateCase("PureBasic")

Input()

Re: PermuateCase

Posted: Wed Apr 17, 2013 9:17 am
by Kwai chang caine
You have right, perhaps that can be useful a day
Thanks for sharing 8)