Compare two strings with same characters, but different order.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am looking to see if there is a quick and dirty function to compare Two strings with all the same characters but different order of charactrers. I don't think i can resort to a simple ascii value because the numerical value of two different strings might add up to the same value
example: cat = atc
 
What is wrong with

if str1 <> str2 then
'do something

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

chris said:
I am looking to see if there is a quick and dirty function to compare Two
strings with all the same characters but different order of charactrers. I
don't think i can resort to a simple ascii value because the numerical value
of two different strings might add up to the same value.
 
I'd sort the characters of each word and then compare them. Neither dirty
nor quick if the strings are long.
--
HTH. Best wishes Harald
Followup to newsgroup only please

chris said:
I am looking to see if there is a quick and dirty function to compare Two
strings with all the same characters but different order of charactrers. I
don't think i can resort to a simple ascii value because the numerical value
of two different strings might add up to the same value.
 
chris,

Try the code below.

HTH,
Bernie
MS Excel MVP

Sub TestCompare()
Dim Str1 As String
Dim Str2 As String

Str1 = "act"
Str2 = "cat"
If CompareWords(Str1, Str2) Then
MsgBox "They're the same"
Else
MsgBox "They're different"
End If
End Sub


Function CompareWords(Str1 As String, Str2 As String) As Boolean
CompareWords = (Sorter(Str1) = Sorter(Str2))
End Function


Function Sorter(InWord) As String
Dim myLetters() As String
Dim myLen As Integer

myLen = Len(InWord)
ReDim myLetters(1 To myLen)

Sorter = ""

For i = 1 To myLen
myLetters(i) = Mid(InWord, i, 1)
For j = 1 To i
If myLetters(i) > myLetters(j) Then
Temp = myLetters(i)
myLetters(i) = myLetters(j)
myLetters(j) = Temp
End If
Next j
Next i

For i = 1 To myLen
Sorter = myLetters(i) & Sorter
Next i

End Function




chris said:
I am looking to see if there is a quick and dirty function to compare Two
strings with all the same characters but different order of charactrers. I
don't think i can resort to a simple ascii value because the numerical value
of two different strings might add up to the same value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top