Posts: 2,169
Threads: 222
Joined: Apr 2022
Reputation:
103
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
PS Love the new perks in the Quick Reply. Good job!!!
Posts: 200
Threads: 5
Joined: Apr 2022
Reputation:
22
11-18-2022, 03:42 AM
(This post was last modified: 11-18-2022, 03:43 AM by JRace.)
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)
Posts: 2,169
Threads: 222
Joined: Apr 2022
Reputation:
103
Posts: 652
Threads: 96
Joined: Apr 2022
Reputation:
22
11-18-2022, 05:45 AM
(This post was last modified: 11-18-2022, 05:45 AM by PhilOfPerth.)
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.
Posts: 2,169
Threads: 222
Joined: Apr 2022
Reputation:
103
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.
Posts: 2,696
Threads: 327
Joined: Apr 2022
Reputation:
217
SWAP is great for use in sort routines.
Posts: 3,964
Threads: 176
Joined: Apr 2022
Reputation:
219
11-18-2022, 01:02 PM
(This post was last modified: 11-18-2022, 01:19 PM by bplus.)
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 + ...
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
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.
Posts: 1,586
Threads: 59
Joined: Jul 2022
Reputation:
52
(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.
Posts: 3,964
Threads: 176
Joined: Apr 2022
Reputation:
219
11-18-2022, 03:08 PM
(This post was last modified: 11-18-2022, 03:09 PM by bplus.)
(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 + ...
|