udfStrReverse
str udfStrReverse (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse (strString)
If strString == "" Then Return ""
arrA = ArrayFromStr (" " : strString)
intLen = ArrInfo (arrA, 1)
intMid = intLen / 2
For intLeft = 1 To intMid
   intRight = intLen - intLeft
   arrA[intLeft - 1] = arrA[intRight]
   arrA[intRight] = arrA[intLeft]
Next
strString = ArrayToStr (arrA)
Return StrSub (strString, 1, intMid) : StrSub (strString, intMid + 2, intRight)
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20091225.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

If Param0 Then If Param1 == ".." Then Return ; Leave this script here when called by another script with specified parameter.


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_1 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
strOut = ""
For intI = intLen To 1 By -1
   strOut = strOut : StrSub (strString, intI, 1)
Next
Return strOut
;..........................................................................................................................................
; Returns string reversed.
; Backward reading.
; "0123456789" <==> "9876543210"
;
; Alternative code lines:
; strOut = StrCat (strOut, StrSub (strString, intI, 1))
; strOut = strOut : StrSub (strString, intI, 1)
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_2 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
intMid = intLen / 2
intBBSize = intLen + 1
hdlBB = BinaryAlloc (intBBSize)
BinaryPokeStr (hdlBB, 1, strString)
For intLeft = 1 To intMid
   intRight = intBBSize - intLeft
   BinaryCopy (hdlBB, 0, hdlBB, intRight, 1)
   BinaryCopy (hdlBB, intRight, hdlBB, intLeft, 1)
   BinaryCopy (hdlBB, intLeft, hdlBB, 0, 1)
Next
strString = BinaryPeekStr (hdlBB, 1, intLen)
hdlBB = BinaryFree (hdlBB)
Return strString
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_3 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
intMid = intLen / 2
intBBSize = intLen + 1
hdlBB = BinaryAlloc (intBBSize)
BinaryPokeStr (hdlBB, 1, strString)
For intLeft = 1 To intMid
   intRight = intBBSize - intLeft
   BinaryCopy (hdlBB, intLeft - 1, hdlBB, intRight, 1)
   BinaryCopy (hdlBB, intRight, hdlBB, intLeft, 1)
Next
strString = BinaryPeekStr (hdlBB, 0, intMid) : BinaryPeekStr (hdlBB, intMid + 1, intRight)
hdlBB = BinaryFree (hdlBB)
Return strString
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Alternative code lines:
; strString = StrCat (BinaryPeekStr (hdlBB, 0, intMid), BinaryPeekStr (hdlBB, intMid + 1, intRight))
; strString = BinaryPeekStr (hdlBB, 0, intMid) : BinaryPeekStr (hdlBB, intMid + 1, intRight)
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_4 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
hdlBB = BinaryAlloc (intLen)
BinaryPokeStr (hdlBB, 0, strString)
intLenHigh = intLen - 2
For IntK = intLenHigh To 0 By -1
   For intI = 0 To intK
      BinaryPoke (hdlBB, intI, BinaryPoke (hdlBB, intI + 1, BinaryPeek (hdlBB, intI)))
   Next
Next
strString = BinaryPeekStr (hdlBB, 0, intLen)
hdlBB = BinaryFree (hdlBB)
Return strString
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_5 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
arrA = ArrDimension (intLen + 1)
For intI = 1 To intLen
   arrA [intI] = StrSub (strString, intI, 1)
Next
intLenHigh = intLen - 1
For intK = intLenHigh To 1 By -1
   For intI = 1 To intK
      ArraySwapElements (arrA, intI, 0, 0, 0, 0, intI + 1, 0, 0, 0, 0)
   Next
Next
strString = ""
For intI = 1 To intLen
   strString = strString : arrA [intI]
Next
Return strString
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Alternative code lines:
; strString = StrCat (strString, arrA [intI])
; strString = strString : arrA [intI]
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_6 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
intI = 1
intK = intLen
While intI < intK
   strChar = StrSub (strString, intI, 1)
   strString = StrSub (strString, 1, intI - 1) : StrSub (strString, intK, 1) : StrSub (strString, intI + 1, -1)
   strString = StrSub (strString, 1, intK - 1) : strChar : StrSub (strString, intK + 1, -1)
   intI = intI + 1
   intK = 1 + intLen - intI
EndWhile
Return strString
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Alternative code lines:
; strString = StrCat (StrSub (strString, 1, intI - 1), StrSub (strString, intK, 1), StrSub (strString, intI + 1, -1))
; strString = StrCat (StrSub (strString, 1, intK - 1), strChar, StrSub (strString, intK + 1, -1))
;
; strString = StrSub (strString, 1, intI - 1) : StrSub (strString, intK, 1) : StrSub (strString, intI + 1, -1)
; strString = StrSub (strString, 1, intK - 1) : strChar : StrSub (strString, intK + 1, -1)
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_7 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
strReverse = ""
For intI = 1 To intLen
   strReverse = strReverse : StrSub (strString, intLen, 1)
   strString = " " : strString
Next
Return strReverse
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20090625.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_8 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
intBBSize = (2 * intLen - 1) * intLen
hdlBB = BinaryAlloc (intBBSize)
BinaryPokeStr (hdlBB, 0, StrFill (strString, intBBSize))
intLast = intLen - 1
strReverse = ""
For intI = 1 To intLen
   strReverse = strReverse : BinaryPeekStr (hdlBB, intI * intLast, 1)
Next
hdlBB = BinaryFree (hdlBB)
Return strReverse
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20090625.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_9 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
strTemp = StrFill (strString, (2 * intLen - 1) * intLen)
intLast = intLen - 1
strReverse = ""
For intI = 1 To intLen
   strReverse = strReverse : StrSub (strTemp, intI * intLast + 1, 1)
Next
Return strReverse
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20090625.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_10 (strString)
If strString == "" Then Return ""
intLen = StrLen (strString)
intMid = (intLen / 2) - 1
intHigh = intLen - 1
hdlBB = BinaryAlloc (intLen)
BinaryPokeStr (hdlBB, 0, strString)
For intI = 0 To intMid
   BinaryXor (hdlBB, intI, hdlBB, intHigh - intI, 1)
   BinaryXor (hdlBB, intHigh - intI, hdlBB, intI, 1)
   BinaryXor (hdlBB, intI, hdlBB, intHigh - intI, 1)
Next
strReverse = BinaryPeekStr (hdlBB, 0, intLen)
hdlBB = BinaryFree (hdlBB)
Return strReverse
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20090707.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_11 (strString)
If strString == "" Then Return ""
arrA = ArrayFromStr (strString)
intLast = ArrInfo (arrA, 1) - 2
For intK = intLast To 0 By -1
   For intI = 0 To intK
      ArraySwapElements (arrA, intI, 0, 0, 0, 0, intI + 1, 0, 0, 0, 0)
   Next
Next
Return ArrayToStr (arrA)
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20091225.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrReverse_12 (strString)
If strString == "" Then Return ""
arrA = ArrayFromStr (" " : strString)
intLen = ArrInfo (arrA, 1)
intMid = intLen / 2
For intLeft = 1 To intMid
   intRight = intLen - intLeft
   arrA[intLeft - 1] = arrA[intRight]
   arrA[intRight] = arrA[intLeft]
Next
strString = ArrayToStr (arrA)
Return StrSub (strString, 1, intMid) : StrSub (strString, intMid + 2, intRight)
;..........................................................................................................................................
; Returns string reversed.
; "0123456789" <==> "9876543210"
;
; Detlev Dalitz.20091225.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsStrPalindrome (strString)
If strString == "" Then Return @FALSE
intLen = 1 + StrLen (strString)
intMid = intLen / 2
For intI = 1 To intMid
   If StrSub (strString, intI, 1) != StrSub (strString, intLen - intI, 1) Then Return @FALSE
Next
Return @TRUE
;..........................................................................................................................................
; This Function "udfIsPalindrome" returns a boolean value
; which indicates if a given string is a Palindrome or not.
; A Palindrome is a string which can be read either from left to right and from right to left.
; For Example: "madam".
;
; Detlev Dalitz.20090523.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

Goto Performancetest

:Test1
;IsPalindrome1 = udfIsStrPalindrome ("madam")
;IsPalindrome2 = udfIsStrPalindrome ("mad adam")
;IsPalindrome3 = udfIsStrPalindrome ("otto")


:Test2
; strTest = "REGEN"
; strTest = "EINNEGERMITGAZELLEZAGTIMREGENNIE"
; strTest = "RADAR"
; strTest = "CIVIC"
; strTest = "ABBA"
; strTest = "A man, a plan, a canal, Panama."
; strTest = "madam"
; strTest = "Lid off a daffodil"
; strTest = "Norma is as selfless as I am, Ron"
strTest = "0123456789A"

;str1 = udfStrReverse_1 (strTest)
;str2 = udfStrReverse_2 (strTest)
;str3 = udfStrReverse_3 (strTest)
;str4 = udfStrReverse_4 (strTest)
;str5 = udfStrReverse_5 (strTest)
;str6 = udfStrReverse_6 (strTest)
;str7 = udfStrReverse_7 (strTest)
;str8 = udfStrReverse_8 (strTest)
;str9 = udfStrReverse_9 (strTest)
;str10 = udfStrReverse_10 (strTest)
;str11 = udfStrReverse_11 (strTest)
str12 = udfStrReverse_12 (strTest)

Exit


:Performancetest

strMsgTitle = "Demo: udfStrReverse (strString)   Performance Test"
BoxOpen (strMsgTitle, "")
WinPlace (300, 200, 700, 600, "")

;strTest = StrFill ("RADAR", 500)
strTest = StrFill ("RADAR", 100)
;strTest = StrFill ("RADAR", 50)
;strTest = StrFill ("RADAR", 10)
intTestLen = StrLen (strTest)

intLoopMax = 10

;blnExclude = @FALSE
blnExclude = @TRUE

:TestLoop
intTestMax = 12
For intTest = 1 To intTestMax
   intTicks%intTest% = 0
   If blnExclude
      blnContinue = @FALSE
      Switch intTest
      Case 4
      Case 5
      Case 11
      Case 9
      Case 6
         blnContinue = @TRUE
      EndSwitch
      If blnContinue Then Continue
   EndIf
   BoxText ("Running Test %intTest%, please wait ...")
   Exclusive (@ON)
   intTicksStart = GetTickCount ()
   For intLoop = 1 To intLoopMax
      strResult = udfStrReverse_%intTest% (strTest)
   Next
   intTicksStop = GetTickCount ()
   Exclusive (@OFF)
   intTicks%intTest% = intTicksStop - intTicksStart
Next


:Result
intTicksMax = 0
For intTest = 1 To intTestMax
   intTicksMax = Max (intTicksMax, intTicks%intTest%)
Next
For intTest = 1 To intTestMax
   intPct%intTest% = 100 * intTicks%intTest% / intTicksMax
Next
strMsgText = ""
For intTest = 1 To intTestMax
   If intTicks%intTest% == 0 Then Continue
   strMsgText = strMsgText : "Test " : intTest : @TAB : "Ticks = " : @TAB : intTicks%intTest% : @TAB : intPct%intTest% : " %%" : @LF
Next

strMsgText = "TestLen = " : intTestLen : @LF : strMsgText

BoxButtonDraw (1, 1, "&OK", "20,780,980,950")
BoxText (strMsgText)
While !BoxButtonStat (1, 1)
   TimeDelay (0.2)
EndWhile
ClipPut (strMsgText)

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;   TestLen = 100
;   Test 1   Ticks =    250   1 %
;   Test 2   Ticks =    250   1 %
;   Test 3   Ticks =    203   1 %
;   Test 4   Ticks =    16407   100 %
;   Test 5   Ticks =    7578   46 %
;   Test 6   Ticks =    468   2 %
;   Test 7   Ticks =    313   1 %
;   Test 8   Ticks =    250   1 %
;   Test 9   Ticks =    640   3 %
;   Test 10   Ticks =    281   1 %
;   Test 11   Ticks =    7219   43 %
;   Test 12   Ticks =    188   1 %       <== The winner.
;------------------------------------------------------------------------------------------------------------------------------------------
;   TestLen = 500
;   Test 1   Ticks =    1109   70 %
;   Test 2   Ticks =    1172   74 %
;   Test 3   Ticks =    968   61 %
;   Test 7   Ticks =    1578   100 %
;   Test 8   Ticks =    1250   79 %
;   Test 10   Ticks =    1312   83 %
;   Test 12   Ticks =    828   52 %      <== The winner.
;------------------------------------------------------------------------------------------------------------------------------------------
;   TestLen = 100
;   Test 1   Ticks =    234   78 %
;   Test 2   Ticks =    250   84 %
;   Test 3   Ticks =    203   68 %
;   Test 7   Ticks =    297   100 %
;   Test 8   Ticks =    250   84 %
;   Test 10   Ticks =    281   94 %
;   Test 12   Ticks =    172   57 %      <== The winner.
;------------------------------------------------------------------------------------------------------------------------------------------
;   TestLen = 50
;   Test 1   Ticks =    125   79 %
;   Test 2   Ticks =    141   89 %
;   Test 3   Ticks =    125   79 %
;   Test 7   Ticks =    157   100 %
;   Test 8   Ticks =    125   79 %
;   Test 10   Ticks =    156   99 %
;   Test 12   Ticks =    94    59 %      <== The winner.
;------------------------------------------------------------------------------------------------------------------------------------------
;   TestLen = 10
;   Test 1   Ticks =    31      65 %
;   Test 2   Ticks =    46      97 %
;   Test 3   Ticks =    47      100 %
;   Test 7   Ticks =    46      97 %
;   Test 8   Ticks =    47      100 %
;   Test 10   Ticks =    46      97 %
;   Test 12   Ticks =    31      65 %
;------------------------------------------------------------------------------------------------------------------------------------------