Level 1, lesson 10

# 1.10 Appending strings

There is no standard word in 4tH to concatenate strings. As a matter of fact, string manipulation is one of 4tHs weakest points. But since we are focused here on doing things, we will present you a word which will get the work done.

The word 'APPEND$' appends two strings. In this example string "one" holds the first name and string "two" holds the last name. String "two" is appended to string "one" to form the full name. Finally string "one" is printed.

         : append$                       ( a1 a2 --)
               count                     \ get length of second string
               dup >r                    \ move copy to return stack
               rot count                 \ get length of first string
               over over                 \ move copy to return stack
               >r >r +                   \ get addr of terminator
               swap cmove                \ append second string
               0 r>                      \ NULL char and addr on stack
               r> r> +                   \ get offset of new terminator
               chars +                   \ calculate addr of terminator
               c!                        \ store terminator
         ;

         32 string one                   \ define string one
         32 string two                   \ define string two

         " Hans " one copy               \ initialize first string
         dup                             \ duplicate address
         " Bezemer" two copy             \ initialize second string
         append$                         \ append both strings
         count type cr                   \ print first string