12-26-2025, 09:50 PM
Just for fun, and maybe someone else already did this (I'm late tot the party)...
Like I said, just for fun, because it isn't bullet proof. Since it uses INSTR() we can thuck fings up by working with sentences that have replacement parts repeated in the sentence. In other words, INSTR() would always pick the first occurrence, even if that's not the one your wanted swapped in the sentence.
Now if you were okay with not making the sub-strings, as you called them, hard coded, you could DIM shared the variables used to determine those sub-strings and write the algorithm differently. Like I said, this was fun, because I decided to see if I could come up with anything using those hard coded values, and I sorta did.
Pete
Oh, and swap that first sentence back before Steve and I sue your ASCII!
Code: (Select All)
Dim Shared t$
t$ = "Pete's Tremendous. Steve's Amazing.": Print t$
swapx Mid$(t$, 8, 10), Mid$(t$, 28, 7)
Print t$
Print
t$ = "My apple is green. Your tomatoe is red.": Print t$
swapx Mid$(t$, 4, 5), Mid$(t$, 25, 7)
Print t$
Sub swapx (b$, a$)
i = Len(t$) - (InStr(t$, a$) - 1 + Len(a$))
j = InStr(t$, b$) - 1
k = j + Len(b$) + 1
temp1$ = Mid$(t$, 1, j) + a$
temp2$ = Mid$(Mid$(t$, k), 1, Len(Mid$(t$, k)) - Len(a$) - i) + b$
t$ = temp1$ + temp2$ + Mid$(t$, Len(temp1$) + Len(temp2$) + 1)
End Sub
Like I said, just for fun, because it isn't bullet proof. Since it uses INSTR() we can thuck fings up by working with sentences that have replacement parts repeated in the sentence. In other words, INSTR() would always pick the first occurrence, even if that's not the one your wanted swapped in the sentence.
Now if you were okay with not making the sub-strings, as you called them, hard coded, you could DIM shared the variables used to determine those sub-strings and write the algorithm differently. Like I said, this was fun, because I decided to see if I could come up with anything using those hard coded values, and I sorta did.
Pete
Oh, and swap that first sentence back before Steve and I sue your ASCII!

