- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
Chapter 8: Crunching Numbers and Playing with Strings 105
LEN(“Greetings from Mars!”)
In this example, the length of the string is 20 characters, including spaces and the exclamation point. Run the following program to see how the LEN function works:
PROMPT “Type a string:”; CountMe$ TotalLength = LEN(CountMe$)
PRINT “The total number of characters in your string is:” PRINT TotalLength
END
So if you run this program and type in the string Beware of copycat publishers, you’ll see the following:
The total number of characters in your string is:
28
In calculating the total length of a string, the LEN function removes any spaces before or after any visible characters, as shown in Table 8-3.
Table 8-3 How the LEN Function Counts Spaces in a String
String |
String Length |
“ Hello!” |
6 characters (eliminates the five spaces in front of |
|
the string) |
“What are you looking at? “ 24 characters (eliminates the five spaces at the end
|
of the string) |
“Boo! Go away!” |
13 characters (counts the spaces between the |
|
words) |
Trimming the front and back of a string
Because strings can have spaces before (known as leading spaces) or after (known as trailing spaces) any visible character, you may want to eliminate these leading or trailing spaces before counting the length of a string.
Fortunately, Liberty BASIC includes a special function to do just that, as the following example shows:
TRIM$(“ Hello, there!”) ‘ “Hello, there!”
TRIM$(“Good-bye! “) ‘ “Good-bye”
106 Part II: Learning Programming with Liberty BASIC
To see how the TRIM$ function can strip away leading and trailing spaces, try the following program:
AString$ = “ Hello, there!”
PRINT “The original length of the string = “; LEN(AString$)
TempString$ = TRIM$(AString$)
PRINT TempString$
PRINT “The new length is now = “; LEN (TempString$)
END
If you run this program, this is what you see:
The original length of the string = 14
Hello, there!
The new length is now = 13
Inserting spaces
Sometimes you may want to create spaces without pressing the spacebar multiple times to do so. For an easy way to create spaces, Liberty BASIC provides the SPACE$ function, which looks as follows:
SPACE$(X)
In this example, X represents the number of spaces that you want to insert. The SPACE$ function in the following program simply inserts five spaces between the string Hello and the string Bo the Cat.:
AString$ = “Bo the Cat.”
PRINT “Hello” + SPACE$(5) + AString$
END
Yanking characters out of a string
If you have a long string, you may want only part of that string. You may have a string consisting of somebody’s first and last name, for example, but you want only the last name. You can use one of the following Liberty BASIC functions to rip away one or more characters from a string:
LEFT$ (string, X) rips away X number of characters starting from the left of the string.
RIGHT$ (string, X) rips away X number of characters starting from the right of the string.
MID$ (string, X, Y) rips away Y number of characters, starting at the Xth character from the left.
Chapter 8: Crunching Numbers and Playing with Strings 107
To see how these three functions work, run the following program and see what happens:
FakeName$ = “John Barkley Doe”
FirstName$ = LEFT$(FakeName$, 4)
PRINT “This is the first name = “; FirstName$
LastName$ = RIGHT$(FakeName$, 3)
PRINT “This is the last name = “; LastName$
MiddleName$ = MID$(FakeName$, 6, 7)
PRINT “This is the middle name = “; MiddleName$
END
This program does nothing more than strip out the first, last, and middle names from the longer string John Barkley Doe.
Looking for a string inside another string
If you have a long string, you may want to know the location of another word or phrase inside the longer string. Suppose, for example, that you had a string containing a list of names, as in the following example:
“John, Julia, Matt, Mike, Sam, Chris, Karen”
If you want to know in what position the name “Matt” appears in the preceding string, you can use the magical INSTR command, as follows:
Names$ = “John, Julia, Matt, Mike, Sam, Chris, Karen”
Position = INSTR(Names$, “Matt”, 1)
PRINT “The name Matt is located in position = “; Position END
This Liberty BASIC program tells the computer to do the following:
1.The first line creates the string variable Names$ that stores the string
John, Julia, Matt, Mike, Sam, Chris, Karen.
2.The second line tells the computer to set the value of the Position variable to the value that the INSTR function returns.
The INSTR function tells the computer, “Starting at position 1, look at the string that the variable Names$ represents and look for the string Matt.” In this case, the string Matt appears in position 14 in the Names$ string.
3. The third line prints The name Matt is located in position = 14.
4. The fourth line tells the computer that the program is at an end.
108 Part II: Learning Programming with Liberty BASIC
To use the INSTR function, you need to specify the following three items:
The position where you want to start searching.
In the preceding example, the position is 1, which represents the start of the string. You can, however, tell the computer to start looking from any position. If you tell the computer to start looking from position 20, it can never find the name “Matt” in the Names$ variable.
The string that you want to search.
The string that you want to locate.
Strings are case-sensitive, which means that, to the computer, the strings “MATT” and “Matt” are two completely different strings. In the following program, INSTR can’t find the string “MATT” because it thinks the strings “MATT” and “Matt” aren’t the same:
Names$ = “John, Julia, Matt, Mike, Sam, Chris, Karen”
Position = INSTR(Names$, “MATT”, 1)
PRINT “The name Matt is located in position = “; Position
END
If the INSTR function can’t find the string that you’re looking for, the value of the INSTR function is zero.
Converting strings into numbers
(and vice versa)
A string can consist of letters, symbols, and numbers. The most common use for storing numbers as a string is if the numbers represent something special, such as a telephone number or address. If you want the computer to print a telephone number, you must store that number as a string, as follows:
PRINT “555-1212”
This command prints the string 555-1212 on-screen. The quotation marks tell the computer, “Anything inside the quotation marks is a string.” What happens if you try the following command?
PRINT 555-1212
Liberty BASIC interprets this command to mean, “Subtract the number 1,212 from the number 555 and print the result (which is –657) on-screen.”
If you have a number but you want to treat it as a string, you can use Liberty BASIC’s STR$ function, which works as follows:
Chapter 8: Crunching Numbers and Playing with Strings 109
STR$(Number)
The STR$ function tells Liberty BASIC, “Take a number (such as 45) and turn it into a string.” If you have a number 34 and use the STR$ function, Liberty BASIC converts that number 34 into a string “34”.
To print both a string and a number, for example, you normally need to use the PRINT command with a semicolon, as in the following example:
BossIQ = 12
PRINT “This is the IQ of your boss =”; BossIQ
END
You can, however, use the STR$ function to convert the BossIQ variable into a string, as follows:
BossIQ = 12
NewString$ = “This is the IQ of your boss = “ + STR$(BossIQ)
PRINT NewString$
END
The main difference between the first example and the second example is that the number BossIQ is now part of the string NewString$.
Naturally, Liberty BASIC enables you to convert strings into numbers as well. If you have a string (such as “46”), you can’t perform any mathematical operations on it. You can, however, convert that string into a number by using Liberty BASIC’s VAL function, which works as follows:
VAL(“String”)
The VAL function tells Liberty BASIC, “Take a string (such as “45”) and turn it into a number.” If you have a string “45” and use the VAL function, Liberty BASIC converts that string “45” into the number 45.
If you use the VAL function on a string that doesn’t represent a number, such as the string “Hello!”, the VAL function returns a zero value.
To see how the VAL function works, run the following program:
YearBorn$ = “1964”
PROMPT “You were born in “; YearBorn$
Year = 2005 – VAL(YearBorn$)
NOTICE “In 2005, you were this old = “; Year
END
If you run this program, the program displays a Prompt dialog box that says, You were born in 1964. Then it displays a Notice dialog box that says, In 2005, you were this old = 35.
110 Part II: Learning Programming with Liberty BASIC