@PhilOfPerth :
Actually, you only need to introduce one new temp var:
Off the top of my head I don't see a way to swap characters within a single string without some old-fashioned Varptr/Peek/Poke shenanigans, and I don't know how easy that would be to pull off in QB64.
Anyway, here's a slight mod to your original code snippet:
(Wow, this post was brand new with no responses when I pulled it up. That's what I get for taking a break.)
Quote:But if I don't use the SWAP function, I can just do this:
Code: (Select All)a$ = "ABCDEFG"
Print a$
t1$ = Mid$(a$, 3, 1): t2$ = Mid$(a$, 5, 1)
Mid$(a$, 3, 1) = t2$: Mid$(a$, 5, 1) = t1$
Print a$
It introduces two new variables, but that's ok in my case.
Actually, you only need to introduce one new temp var:
Code: (Select All)
t1$ = Mid$(a$, 3, 1)
Mid$(a$, 3, 1) = Mid$(a$, 5, 1)
Mid$(a$, 5, 1) = t1$
Off the top of my head I don't see a way to swap characters within a single string without some old-fashioned Varptr/Peek/Poke shenanigans, and I don't know how easy that would be to pull off in QB64.
Anyway, here's a slight mod to your original code snippet:
Code: (Select All)
Screen 9
_FullScreen
a$ = "ABCDEFG"
Print Mid$(a$, 3, 1), Mid$(a$, 5, 1)
'The fourth parameter allows you to swap more than one character at a time:
myswap a$, 3, 5, 1 ' swap mid$(a$,3,1),mid$(a$,5,1)
'Below is the swapping code for inline use, if desired.
't$ = Mid$(a$, 3, 1): Mid$(a$, 3, 1) = Mid$(a$, 5, 1): Mid$(a$, 5, 1) = t$
Print a$
Sub myswap (s$, e1, e2, l)
t$ = Mid$(s$, e1, l)
Mid$(s$, e1, l) = Mid$(s$, e2, l)
Mid$(s$, e2, l) = t$
End Sub
(Wow, this post was brand new with no responses when I pulled it up. That's what I get for taking a break.)