Skip to content
Lesson 6 of 6

Step 1 of 5 · Reading · ~4 min

Learn

Subs, Functions, Arrays

Strings and PRINT Formatting

By the end of this lesson you can take text apart and rebuild it with FreeBASIC's string functions, convert between characters and their numeric codes, and control exactly how PRINT spaces its output.

Taking a string apart

DIM s AS STRING = "starship"

LEN(s)               ' 8
LEFT(s, 4)           ' "star"
RIGHT(s, 4)          ' "ship"
MID(s, 3, 3)         ' "ars"   (start, length)
MID(s, 5)            ' "ship"  (from the start position to the end)
UCASE(s)             ' "STARSHIP"
TRIM("  hi  ")       ' "hi"    (LTRIM and RTRIM strip one side only)
INSTR(s, "ship")     ' 5
INSTR(2, s, "s")     ' 5       (start looking at position 2)
INSTR(s, "x")        ' 0

Two habits to build. Positions are 1-indexed: the first character is position 1, the opposite of the C family. And "not found" is 0, not -1: with no position 0 on offer, zero is free to mean failure. IF INSTR(s, x) >= 0 is therefore always true and always a bug.

Joining and converting

PRINT "star" & "ship"          ' starship   (& joins text and converts numbers)
PRINT STR(42) & "!"            ' 42!        (STR gives text with no sign column)
PRINT STR(VAL("42kg") + 1)     ' 43         (VAL reads the leading number)
PRINT STR(VAL("abc"))          ' 0          (no number found)
PRINT STRING(5, "=")           ' =====
PRINT "She said ""hi"""        ' She said "hi"   (a doubled quote is one quote)

+ also joins text, but only when both sides are strings, so prefer &. Comparing strings with =, < and > compares character codes, so it is case-sensitive: "a" = "A" is false. Compare UCASE(a) with UCASE(b) for a case-insensitive test.

Characters are numbers

Every character has a numeric code. ASC turns a character into its code and CHR turns a code back into a character:

ASC("A")             ' 65
CHR(66)              ' "B"
CHR(72) & CHR(105)   ' "Hi"
ASC("7") - ASC("0")  ' 7, the digits 0 to 9 have consecutive codes 48 to 57

Capital letters are codes 65 to 90 and lowercase letters 97 to 122. That lets a loop classify characters by range. This counts the digits in a string:

DIM digits AS INTEGER = 0
DIM c AS INTEGER, i AS INTEGER
FOR i = 1 TO LEN(s)
    c = ASC(MID(s, i, 1))
    IF c >= 48 AND c <= 57 THEN digits = digits + 1
NEXT i

When a position has to wrap around a fixed cycle, MOD does it. On a 12-hour clock, 5 hours after 9 o'clock is (9 + 5) MOD 12, which is 2.

PRINT: the separator decides the spacing

You met ; and , in the first lesson. With text they behave as described, but a number printed after ; still gets its sign column:

DIM age AS INTEGER = 36
PRINT "Ada is "; age; " years old"    ' Ada is  36 years old   (two spaces)
PRINT "Ada is " & age & " years old"  ' Ada is 36 years old

The comma still jumps to the next 14-column zone. PRINT USING formats numbers into a fixed pattern, where each # is a digit position:

PRINT USING "###.##"; 3.14159         ' "  3.14", 6 characters wide

It pads, so for grader-exact output & is the safer tool. FreeBASIC has no triple-quoted strings; build several lines with one PRINT per line.

Common mistakes

  • Starting at position 0. The first character is MID(s, 1, 1).
  • Testing a character range with a bound that is off by one. The characters right next to the letters are @ and [ before and after the capitals, and the backtick and { around the lowercase ones. They are not letters.
  • Forgetting the wrap. A code pushed past Z becomes [, \ and so on, not A.
  • Rebuilding a string but dropping the characters you did not change. The result must contain every character, in order.
  • Joining a number with ; and getting a double space.

Your exercise

Write a Caesar cipher: move every letter forward through the alphabet by a given amount, wrapping at the end, and leave everything else alone. The starter reads the text and the amount, walks the text one character at a time, and rebuilds it unchanged; the shifting goes inside that loop.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…