• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Equivalent of STReverse statement

Started by Chris Chancellor, May 03, 2018, 05:29:43 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

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

What is the equivalent for this command in O2

Charles Pegge

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"

Patrice Terrier


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--;
    }
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Chris Chancellor

Thanxx a lot Charles and Patrice

you guys are maestros

Charles Pegge



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




Charles Pegge

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