reversing_20a_20string
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
reversing_20a_20string [2018/03/31 13:19] – external edit 127.0.0.1 | reversing_20a_20string [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
//by Jon Ripley, May 2006//\\ \\ Reversing is a simple string manipulation that often has a sub-optimal solution.\\ \\ One commonly used method to reverse strings is:\\ | //by Jon Ripley, May 2006//\\ \\ Reversing is a simple string manipulation that often has a sub-optimal solution.\\ \\ One commonly used method to reverse strings is:\\ | ||
+ | <code bb4w> | ||
a$=" | a$=" | ||
IF LEN(a$)> | IF LEN(a$)> | ||
Line 12: | Line 13: | ||
ENDIF | ENDIF | ||
PRINT a$ | PRINT a$ | ||
+ | </ | ||
This method using the built in string function **MID$(** four times and requiring one temporary variable **t$** can be made approximately 4 times faster by improving the algorithm used.\\ \\ It might seem logical to use the **SWAP** command to exchange the characters in the string, however the following code is a syntax error: | This method using the built in string function **MID$(** four times and requiring one temporary variable **t$** can be made approximately 4 times faster by improving the algorithm used.\\ \\ It might seem logical to use the **SWAP** command to exchange the characters in the string, however the following code is a syntax error: | ||
+ | <code bb4w> | ||
SWAP MID$(a$, | SWAP MID$(a$, | ||
+ | </ | ||
The BBC BASIC manual tells us that **SWAP** can exchange the contents of two locations in memory, if we know the address of the string we can manipulate the string directly in memory. To do this use the pointer operator **^** to locate the address of the string in memory. The address in memory of **any$** is **^any$**. The address of the text stored in **any$** is **!^any$**.\\ \\ We can use this information to create the following optimal code to reverse a string: | The BBC BASIC manual tells us that **SWAP** can exchange the contents of two locations in memory, if we know the address of the string we can manipulate the string directly in memory. To do this use the pointer operator **^** to locate the address of the string in memory. The address in memory of **any$** is **^any$**. The address of the text stored in **any$** is **!^any$**.\\ \\ We can use this information to create the following optimal code to reverse a string: | ||
+ | <code bb4w> | ||
a$=" | a$=" | ||
IF a$>"" | IF a$>"" | ||
Line 23: | Line 28: | ||
ENDIF | ENDIF | ||
PRINT a$ | PRINT a$ | ||
+ | </ | ||
Here we loop through the first half of the string and swap the characters with those in the second half of the string.\\ \\ This algorithm can be made significantly faster by rewriting it in assembly language. | Here we loop through the first half of the string and swap the characters with those in the second half of the string.\\ \\ This algorithm can be made significantly faster by rewriting it in assembly language. |
reversing_20a_20string.1522502378.txt.gz · Last modified: 2024/01/05 00:16 (external edit)