Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alphabetical sort of characters within a string.
#2
(04-24-2024, 12:25 PM)Circlotron Wrote: I want to alphabetically sort the characters of a single word string. The only way i can think of so far is to get each character individually and make each one a single character variable, check the ASC value and bubble sort them with a SWAP function. Is there a better way?

another more direct way is to use my anaCode$ function, it turns any word into a sorted listing of letters and their counts in a word that way you can find the anagrams of a word from a dictionary listing of all words

i added a decoder so you can see the alpha breakdown of letters in a word
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
    Dim L(26)
    w$ = UCase$(wrd$)
    For i = 1 To Len(wrd$)
        p = Asc(w$, i) - 64 ' A=1, B=2...
        L(p) = L(p) + 1
    Next
    For i = 1 To 26
        rtn$ = rtn$ + _Trim$(Str$(L(i)))
    Next
    AnaCode$ = rtn$
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
b = b + ...
Reply


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



Users browsing this thread: 4 Guest(s)