How to resolve the algorithm Reverse a string step by step in the FBSL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Reverse a string step by step in the FBSL programming language
Table of Contents
Problem Statement
Take a string and reverse it. For example, "asdf" becomes "fdsa".
Preserve Unicode combining characters. For example, "as⃝df̅" becomes "f̅ds⃝a", not "̅fd⃝sa".
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Reverse a string step by step in the FBSL programming language
Source code in the fbsl programming language
Function StrRev1(ByVal $p1)
dim $b = ""
REPEAT len(p1)
b = b & RIGHT(p1,1)
p1 = LEFT(p1,LEN(p1)-1)
END REPEAT
return b
End Function
Function StrRev2(ByVal $p1)
dim $b = "", %i
for i = len(p1) DOWNTO 1
b = b & MID(p1,i,1)
next
return b
End Function
Function StrRev3( $s )
FOR DIM x = 1 TO LEN(s) \ 2
PEEK(@s + LEN - x, $1)
POKE(@s + LEN - x, s{x})(@s + x - 1, PEEK)
NEXT
RETURN s
end function
DynC StringRev($theString) As String
void rev(char *str)
{
int len = strlen(str);
char *HEAD = str;
char *TAIL = str + len - 1;
char temp;
int i;
for ( i = 0; i <= len / 2; i++, HEAD++, TAIL--) {
temp = *HEAD;
*HEAD = *TAIL;
*TAIL = temp;
}
}
char *main(char* theString)
{
rev(theString);
return theString;
}
End DynC
DYNASM RevStr(BYVAL s AS STRING) AS STRING
// get length of string
// divide by two
// setup pointers to head and tail
// iterate from 1 to (length \ 2)
// swap head with tail
// increment head pointer
// decrement tail pointer
ENTER 0, 0 // = PUSH EBP: MOV EBP, ESP
PUSH EBX // by Windows convention EBX, EDI, ESI must be saved before modification
MOV EAX, s // get string pointer
MOV ECX, EAX // duplicate it
.WHILE BYTE PTR [ECX] <> 0
INC ECX // propagate to tail
.WEND
MOV EDX, ECX // duplicate tail pointer
DEC EDX // set it to last byte before trailing zero
SUB ECX, EAX // get length in ECX in 1 CPU cycle
SHR ECX, 1 // get length \ 2 in 1 CPU cycle; that's the beauty of power-of-two division
.WHILE ECX > 0
MOV BL, [EDX] // no need to XOR; just overwrite BL and BH contents
MOV BH, [EAX] // DynAsm deduces data size from destination register sizes
MOV [EDX], BH // ditto, source register sizes
MOV [EAX], BL
INC EAX // propagate pointers
DEC EDX
DEC ECX // decrement counter
.WEND
// point to start of string again
MOV EAX, s // MOV = 1 CPU cycle, PUSH + POP = 2 CPU cycles
POP EBX // by Windows convention ESI, EDI, EBX must be restored if modified
LEAVE // = POP EBP
RET
END DYNASM
You may also check:How to resolve the algorithm Permutations by swapping step by step in the Scala programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Perl programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the Maple programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the Wren programming language