Step 1 of 5 · Reading · ~3 min
Learn
Subs, Functions, Arrays
Strings and PRINT Formatting
FreeBASIC's string toolkit is QuickBASIC's, plus a few modern additions.
String functions
DIM s AS STRING
s = "hello world"
LEN(s) ' 11
UCASE(s) ' "HELLO WORLD"
LCASE(s) ' "hello world"
LEFT(s, 5) ' "hello"
RIGHT(s, 5) ' "world"
MID(s, 7, 5) ' "world" (start, length — 1-indexed!)
MID(s, 7) ' "world" (to end)
LTRIM(s) ' strip leading whitespace
RTRIM(s) ' strip trailing whitespace
TRIM(s) ' both
INSTR(s, "world") ' 7 (1-indexed position)
INSTR(8, s, "l") ' 10 (search starting from 8)
INSTR(s, "xyz") ' 0 (not found)
Two habits to build here. Positions are 1-indexed — historic legacy, and the opposite of every C-family language. And "not found" is 0, not -1: because there is no position 0, zero is free to mean failure. Writing IF INSTR(s, x) >= 0 is therefore always true and always a bug.
Concatenation and conversion
DIM full AS STRING
full = "Hello, " & "World!" ' & always means "join text"
full = "Hello, " + "World!" ' + works only when BOTH sides are strings
ASC("A") ' 65 (ASCII code)
CHR(65) ' "A"
STR(42) ' "42" — no leading space
VAL("42 hello") ' 42 (parse leading number)
VAL("abc") ' 0 (no number found)
Old QBASIC's STR$ put a leading space before positive numbers; FreeBASIC's STR does not — STR(42) is exactly "42", and negatives keep their -. (The $-suffixed names like STR$/CHR$ are QB-dialect only and will not compile here.) Prefer & over +: it converts numbers for you, where + on a string and a number is a compile-time type mismatch.
PRINT: the separator decides the spacing
Comma — column zones, every 14 characters:
PRINT "a", "b", "c"
' a b c
Semicolon — no space between two strings:
PRINT "a";"b";"c"
' abc
But a number printed with ; still gets its sign column, the leading blank you met in the Variables lesson. This surprises everyone once:
DIM person AS STRING = "Ada"
DIM age AS INTEGER = 36
PRINT person; " is "; age; " years old" ' Ada is 36 years old <- TWO spaces
PRINT person & " is " & age & " years old" ' Ada is 36 years old <- what you meant
; concatenates without adding anything of its own, but PRINT writes each number with a sign column in front. & converts the number to text first, so no sign column ever appears. When exact output matters, build the line with &.
PRINT USING — formatted output
PRINT USING "###.##"; 3.14159 ' " 3.14" (width 6, right-aligned)
PRINT USING "\ \"; "hi" ' "hi " (7-wide string field, LEFT-justified)
The template characters: # is a digit position, 0 a required digit (zero-padded), . the decimal point, +/- a sign, and \...\ a fixed-width string field whose width is the two backslashes plus the gap between them. It is the closest BASIC gets to printf — but it pads, so for grader-exact output & is usually the safer tool.
FORMAT — sprintf-style
FreeBASIC has a FORMAT function; it needs an include:
#include "string.bi"
DIM s AS STRING
s = FORMAT(3.14159, "0.00") ' "3.14"
Multi-line strings
No triple-quote literals. Use CHR(10) for newlines:
DIM block AS STRING
block = "line 1" & CHR(10) & "line 2" & CHR(10) & "line 3"
PRINT block
Your exercise
Reverse the line the starter has read into s, leaving the result in r, which the starter prints. The pieces are all above: LEN tells you how far the string runs, MID(s, i, 1) pulls out the single character at position i — remembering that positions start at 1, not 0 — and & glues characters onto r.
Walking a string backwards needs a countdown, which is STEP -1 on a FOR. The starter declares the loop counter with DIM i AS INTEGER for you: a FOR i = ... whose i was never declared is the compile error this exercise produces more than any other ("Variable not declared, i").
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…