Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alphabetical sort of characters within a string.
#7
i am saying no sorting is needed just count the letters of a word into a string the count for a's is first, b's is 2nd, c's 3rd... you get a coded string with the counts sorted a to z

i modified my anaCode$ to skip the string concatenation to speed up processing;
Code: (Select All)
_Title "AnaCode$ function" ' b+ 2022-11-17 mod 2024-04-24 decodeAnacode sub
test$(0) = "grmaana"
test$(1) = "angiogram"
test$(2) = "naagrma"
test$(3) = "telgram"
test$(4) = "gramana"
test$(5) = "gram"
test$(6) = "nag"
test$(7) = "tag"
test$(8) = "am"
test$(9) = "grip"

For i = 0 To 9
    Print test$(i), AnaCode$(test$(i)),
    decodeAnacode AnaCode$(test$(i))
Next

Function AnaCode$ (wrd$) ' anaCode$ converts word to an Anagram pattern
    ' number of A's in first, number of B's in 2nd, number of C's in third
    s$ = String$(26, "0")
    w$ = UCase$(wrd$)
    For i = 1 To Len(wrd$)
        p = Asc(w$, i) - 64 ' A=1, B=2...
        Mid$(s$, p, 1) = _Trim$(Str$(Val(Mid$(s$, p, 1)) + 1))
    Next
    AnaCode$ = s$
End Function

Sub decodeAnacode (Coded$)
    For i = 1 To 26
        n$ = Mid$(Coded$, i, 1)
        If n$ <> "0" Then Print Chr$(i + 64) + "-" + n$ + " ";
    Next
    Print
End Sub

this code makes only one pass through the word, no sort routine on letters could be faster!

btw i call my code 'ana' because all anagrams of a word code to the same sequence and counts of letters.
b = b + ...
Reply


Messages In This Thread
RE: Alphabetical sort of characters within a string. - by bplus - 04-24-2024, 06:45 PM



Users browsing this thread: 2 Guest(s)