Reverse words in a string: Difference between revisions

Content added Content deleted
Line 837: Line 837:


=={{header|Forth}}==
=={{header|Forth}}==
The method shown is based on submissions in comp.lang.forth. Comments have been added for those less familiar with reading Forth. This example makes use of the internal Forth parser and calls PARSE-NAME which is a Forth 2012 word that parses the input stream, ignores leading spaces and delimits at the space character.
The method shown is based on submissions in comp.lang.forth. Comments have been added for those less familiar with reading Forth. This example makes use of the Forth interpreter/compiler internals by calling PARSE-NAME which is a Forth 2012 word that parses the input stream, ignores leading spaces and delimits at the space character. This method makes clever use of the data stack. Strings are not copied in memory by rather pointers are collected on the stack as PARSE-NAME processes a sentence. This of course leaves the strings in the reversed order so they are simply printed off the data stack.
<lang>create buf 1000 chars allot \ string buffer
<lang>create buf 1000 chars allot \ string buffer
buf value pp \ pp points to buffer address
buf value pp \ pp points to buffer address