Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 17 : String Manipulation (Right/Mid)

The B2C language offers no way to assign one string to another:

dim a[32] as char
dim b[32] as char
:
a = b  'illegal operation

Instead there are 2 statements for copying strings. These are the Mid statement (used for copying the middle of one string to another) and the Right statement (used for copying the right side of a string). The Mid and Right statements takes the form:

Mid destination, source, start [, length]   'the length is optional
Right destination, source, length

For the Mid statement, the 'destination' is a dimensioned string where the result will be stored. The contents of the original string will be discarded. The 'source' is a dimensioned string that holds data to be copied to the destination. The 'start' is a numerical value (variable, constant, or expression) indicating the first character from the 'source' to copy (remember, in B2C we count starting at zero). The optional length indicates how many bytes to copy. If the length is omitted, or if it is larger than the number of characters in the source array, the entire source array is copied.

The Right statement has similar parameters. The destination is the dimensioned string to copy into, and the source is the string to copy from. The length parameter is required and indicates how many letters from the RIGHT of the source string to copy into the destination.

In our example program we will learn to speak pig latin. It is not perfect. Can you think of ways to improve it?

Example Program

DO THIS

Copy the file "c:\…\B2Cv5\tutorial\ch18.b2c" to "C:\…\B2Cv5\ch18.b2c". Then execute the command "build ch18.b2c" Download the ch18.app file to the cybiko
 
'ch 18
dim word[32] as char   ' the word to convert
dim ordway[32] as char ' the resultant word
dim len as int         ' the length of ordway
 
input "enter your word ", word  'input
mid ordway, word, 1             'copy the word but not the 1st char
 
len = 0
for i=0 to 31                   'get the ordway length so we can add 'ay'
    if ordway[i] = 0 and len = 0 then
      len = i
    end if
next
'print len
ordway[len] = word[0]    ' append the first letter of word to the end of ordway
ordway[len+1] = 97       ' append 'a'
ordway[len+2] = 121      ' append 'y'
ordway[len+3] = 0        ' null terminator, very important
 
print word, "=", ordway  ' print results
input word ' wait for Enter