; IsAnagram() par Ar-S / 2011
; More usefull stuff in my Ar-S Lib : http://forumpb.ldvmultimedia.com/index.php?topic=22.msg40
If IsAnagram("PURE BASIC","CUBE SARIP")
MessageRequester("Oh yeah","Both words are anagram")
Else
MessageRequester("Nop !","There is no anagram")
EndIf
looks interesting, but i really try to avoid string operations. I pretty often get some brain error induced bugs with them You could do it like this with an array for example:
Procedure isAnagram(string1.s,string2.s)
Protected len.i = Len(string1)
If len <> Len(string2)
ProcedureReturn #False
EndIf
Dim char1.i(len-1)
Dim char2.i(len-1)
For a=0 To len-1
char1(a) = PeekB(@string1+a)
char2(a) = PeekB(@string2+a)
Next
SortArray(char1(),#PB_Sort_Ascending)
SortArray(char2(),#PB_Sort_Ascending)
For a=0 To len-1
If char1(a) <> char2(a)
ProcedureReturn #False
EndIf
Next
ProcedureReturn #true
EndProcedure
Hi, Thanks for your code,
For the case sensitiv question, it was only numbers in my contest, but it have to.
I simply use a Debug IsAnagram(LCase("EpURE"),LCase("UPREE")) for example.
@Danillo: Your code doesn't clear the global arrays. If you do a test for non-anagrams then all tests are unreliablel afterwards because they are adding to old tallys.
Here's a solution that is similar to Danilo's, though different:
Procedure sortLettersInPlace(*word.Character, wordLength) ;sorts the letters of a word without creating a new string
Protected Dim letters.c(wordLength)
Protected *letAdr = @letters()
CopyMemoryString(*word, @*letAdr)
SortArray(letters(), #PB_Sort_Ascending, 0, wordLength - 1)
CopyMemoryString(@letters(), @*word)
EndProcedure
Procedure isAnagram(SourceString.s, DicoString.s)
sortLettersInPlace(@SourceString, Len(SourceString))
sortLettersInPlace(@DicoString, Len(DicoString))
If SourceString =DicoString
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
If isAnagram("PURE BASIC","CUBE SARIP")
MessageRequester("Yup!","Both words are anagrams.")
Else
MessageRequester("Nop!", "Words are not anagrams.")
EndIf
It works for unicode or ascii and requires no big global arrays.
Demivec wrote:@Danillo: Your code doesn't clear the global arrays. If you do a test for non-anagrams then all tests are unreliablel afterwards because they are adding to old tallys.
Procedure.b IsAnagram(SourceString.s,DicoString.s)
Text$ = DicoString
Text2$ = SourceString
If Len(Text$) > Len(Text2$)
mlen = Len(Text$)
Else
mlen = Len(Text2$)
EndIf
For i = 1 To mlen
If Mid(DicoString,i,1) < Chr(65) Or (Mid(DicoString,i,1) > Chr(90) And Mid(DicoString,i,1) < Chr(97)) Or Mid(DicoString,i,1) > Chr(122)
Text$ = RemoveString(Text$, Mid(DicoString,i,1))
EndIf
If Mid(SourceString,i,1) < Chr(65) Or (Mid(SourceString,i,1) > Chr(90) And Mid(SourceString,i,1) < Chr(97)) Or Mid(SourceString,i,1) > Chr(122)
Text2$ = RemoveString(Text2$, Mid(SourceString,i,1))
EndIf
Next
If Len(Text$) <> Len(Text2$)
ProcedureReturn #False
EndIf
For i = 1 To Len(Text$)
Text2$ = RemoveString(Text2$, Mid(Text$,i,1),0)
;Text2$ = RemoveString(Text2$, Mid(Text$,i,1),1) For Case insensitive remove
Next
If Text2$ =""
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
If IsAnagram("PURE BASIC","CUBE SAR!?IP")
MessageRequester("Oh yeah","Both words are anagram")
Else
MessageRequester("Nop !","There is no anagram")
EndIf
Edit : Updated
Last edited by RASHAD on Mon Jul 25, 2011 9:20 pm, edited 1 time in total.