Hello Charles
In PB there is an STreverse statement which can reverse the contents of a string
Main$ = "my goodness"
s$ = STRREVERSE$(Main$)
? S$ ' ssendoog ym"
see http://www.manmrk.net/tutorials/basic/PowerBASIC/pbcc6/strreverse_function.html (http://www.manmrk.net/tutorials/basic/PowerBASIC/pbcc6/strreverse_function.html)
What is the equivalent for this command in O2
Hi Chris,
This uses byte overlays to minimise string manipulation:
function StrReverse(string s) as string
=======================================
int le=len s
int j=le
int i
string t=nuls le
byte bs at strptr s
byte bt at strptr t
indexbase 1
for i=1 to le
bt[j]=bs[i]
j--
next
return t
end function
'
'TEST:
======
print StrReverse "1234ABCD"
template <class T> void SWAP ( T& a, T& b ) {
T c(a); a=b; b=c;
}
void reverse(WCHAR str[], long length) {
int start = 0;
int end = length -1;
while (start < end) {
SWAP(*(str + start), *(str + end));
start++;
end--;
}
}
Thanxx a lot Charles and Patrice
you guys are maestros
template <class T> void SWAP ( T& a, T& b ) {
T c(a); a=b; b=c;
}
Thanks Patrice,
I wasn't familiar with the template syntax.
In o2, my solution to the intermediary type is to support left-sided typeof to replace the normal dim statement thus:
macro Swap(a,b, c)
=====================
typeof(a) c=a
a=b
b=c
end macro
'
'
swap x,y
Swap can also be expressed as a single liner with or without curly braces:
'compact swap:
macro swap(a,b, c){ typeof(a) c=a : a=b : b=c }
' without curly braces:
'
macro swap(a,b, c) typeof(a) c=a : a=b : b=c
Thanxx Charles