Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swapping array elements
#11
But my FUNCTON is a one-liner...

Code: (Select All)
a$ = "ABCDEFG"

PRINT goswap$(a$, 3, 5)

FUNCTION goswap$ (a$, x, y)
    goswap$ = MID$(a$, 1, x - 1) + MID$(a$, y, 1) + MID$(a$, x + 1, y - (x + 1)) + MID$(a$, x, 1) + MID$(a$, y + 1)
END FUNCTION

Pete Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue

PS Love the new perks in the Quick Reply. Good job!!!
Reply
#12
Alright, dadburn it, you guys made me think!

In-string swap with no temps and no other changes to string:

Code: (Select All)
a$ = "ABCDEFG"

Print a$

Mid$(a$, 3, 1) = Chr$(Asc(Mid$(a$, 3, 1)) Xor Asc(Mid$(a$, 5, 1)))
Mid$(a$, 5, 1) = Chr$(Asc(Mid$(a$, 3, 1)) Xor Asc(Mid$(a$, 5, 1)))
Mid$(a$, 3, 1) = Chr$(Asc(Mid$(a$, 3, 1)) Xor Asc(Mid$(a$, 5, 1)))

Print a$

(https://en.wikipedia.org/wiki/XOR_swap_algorithm)
Reply
#13
[Image: ?u=https%3A%2F%2Fi.gifer.com%2Fembedded%...ipo=images]
Reply
#14
Thanks all, but simplicity is my middle name, and I still think the method I proposed in post #6 is the simplest, even though it uses 2 new variables.
Interesting though that none of the suggestions used the SWAP function. Seems it has limited use, at least where string elements are concerned. Undecided
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#15
The last string operation I used SWAP on was string math routines where comparisons were being made and in some instances the larger of the two needed to be the first variable to be passed to the function, hence: IF  LEN(a$) > LEN(b$) THEN SWAP a$,  b$

Pete
Shoot first and shoot people who ask questions, later.
Reply
#16
SWAP is great for use in sort routines.
Reply
#17
Here is LetterSwap$ Function that actually uses Swap!
Code: (Select All)
a$ = "ABCDEF"
Print "3, 5 "; LetterSwap$(a$, 3, 5)
Print "1, 4 "; LetterSwap$(a$, 1, 4)
Print "4, 2 "; LetterSwap$(a$, 4, 2)

Sub swapLetters (a$, n, m)
    t$ = Mid$(a$, n, 1)
    Mid$(a$, n, 1) = Mid$(a$, m, 1)
    Mid$(a$, m, 1) = t$
End Sub

Function LetterSwap$ (a$, n, m)
    Dim L$(1 To Len(a$))
    For i = 1 To Len(a$)
        L$(i) = Mid$(a$, i, 1)
    Next
    Swap L$(n), L$(m)
    rtn$ = Space$(Len(a$))
    For i = 1 To Len(a$)
        Mid$(rtn$, i, 1) = L$(i)
    Next
    LetterSwap$ = rtn$
End Function

Swap also great for scrambling and shuffling!

And once again, @PhilOfPerth Swap needs 2 variables to switch their values. Letter swapping in a string simply involves one variable.

Neither could you: swap "word1", "word2"
b = b + ...
Reply
#18
Character swap could be done easily in C. Enough said. Otherwise we would have to go into the realm of object-oriented programming. I'm not sure now but Delphi/Free Pascal had a class that allowed an ordinary string to be handled according to its constituent characters, allowed square-bracket operator like "word[1]" instead of like in BASIC "MID$(word$, 2, 1)". This was to access the second character of "word" string variable. Saying "word(1)" instead of "word[1]" probably called the class constructor. What was confusing is that array elements always started at zero, and the bad design decision was taken not to allow low and high array bound definition like a normal Pascal array.
Reply
#19
(11-18-2022, 12:36 AM)SMcNeill Wrote: You might want to swap to ASC over MID$, due to the speed and performance difference between the two commands.
"ASC" might make a good keyword of the day, "my gosh how it's grown" or "not if you went to Dartmouth" or something like that.
Reply
#20
(11-18-2022, 02:49 PM)mnrvovrfc Wrote:
(11-18-2022, 12:36 AM)SMcNeill Wrote: You might want to swap to ASC over MID$, due to the speed and performance difference between the two commands.
"ASC" might make a good keyword of the day, "my gosh how it's grown" or "not if you went to Dartmouth" or something like that.

Yes, it is very interesting that it can be used like the MID$ Statement!!!
Referring to this:
Code: (Select All)
a$ = "ABCDEFG"

Print SwapString(a$, 3, 5)

Function SwapString$ (s_passed$, pos1, pos2)
    s$ = s_passed$ 'so we don't change by parameter
    temp = Asc(s$, pos1)
    Asc(s$, pos1) = Asc(s$, pos2)
    Asc(s$, pos2) = temp
    SwapString$ = s$
End Function

You can alter the ASC's and not bother with CHR$() to convert back to string.

Also I agree with PhilOfPerth that using his 2 variable change is nice simple straight forward solution too.
b = b + ...
Reply




Users browsing this thread: 3 Guest(s)