PermuateCase

Share your advanced PureBasic knowledge/code with the community.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

PermuateCase

Post 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()
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: PermuateCase

Post by Kwai chang caine »

You have right, perhaps that can be useful a day
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply