QBASIC Lesson 2 - String handling Okay, you've had an easy introduction, now we're going to progress onto something useful. Strings. You've already heard about variables, right? A variable is the computer's way of storing a number. But wat about storing a word, a group of words or even a novel? This is where strings get involved. The best way to show you is probably by example, so here is an example: a$ = "Hello World!" PRINT a$ Another completely pointless program, I know, but it does illustrate a point. To use a string simply put the dollar sign after a variable name ($) and the computer will know that it is supposed to be a string. Don't ask me why it's a dollar sign; it's been like that for ages. By the way, a$ is pronounced "a string". So, a string works pretty much like a variable. Strings can be added together as follows: a$ = "Hello " b$ = "World!" c$ = a$ + b$ PRINT c$ However they cannot be multiplied, subtracted or divided. There are other functions which provide virtually anything you could ask for. Experiment with the following until you've worked out what they do (don't expect me to do all the work!): a$ = UCASE$("hello world!") PRINT a$ a$ = LCASE$("HELLO WORLD!") PRINT a$ a$ = RIGHT$("Hello World!", 6) PRINT a$ a$ = LEFT$("Hello World!", 5) PRINT a$ a$ = MID$("Hello World!", 3, 6) PRINT a$ a = LEN("Hello World!") PRINT a Experiment with different numbers where applicable until you've fathomed what the different functions do. These form the basics of string handling in QBASIC. Using these commands along with the "+" command you should be able to create pretty much any string you want. If you get stuck then have a look in the QBASIC help file; however it is often much easier to discover these things by yourself. There is one possible hurdle, however. What if you have a variable, but need to treat it as a string? Or a string, but need to treat it as a variable? No problem. These two commands do exactly that: n$ = "100" a = VAL(n$) PRINT a n = "100" a$ = STR$(n) PRINT a$ In this way, you can take a string and load it's value into a variable, or take a variable and load it's value into a string. This is immensly useful, although at the moment I can't actually think of a useful application. Okay, one final pair of commands before we finish talking and start programming: a$ = CHR$(98) PRINT a$ a = ASC("b") PRINT a You may of heard of the ASCII character codes. ASCII is basically a labelling system where every letter, symbol, in fact every symbol that you can see in text on a computer, is given a number. For example, lower case a is 97. There is a full list of the ASCII characters in the QBASIC help file. Anyway, the command CHR$ produces the character for a particular ASCII code; for example, give it the number 97 and it will give you an a. The command ASC does exactly the opposite. Give it a character and it will give you the ASCII code. Easy. Okay, now for today's program: CLS INPUT "Please enter your surname", n$ INPUT "Please enter your first name", f$ INPUT "Please enter Mr/Mrs/Miss/Ms", t$ n = INT(RND * 10) + 1 p$ = t$ + f$ + n$ + " your number is:" + STR$(n) PRINT p$ All this program does is ask your name and whether you wish to be addressed as Mr, Mrs, Miss or Ms. It then picks a random number and gives it to you, calling you by the name you entered. I think a line-by-line breakdown is probably a good idea at this stage: CLS Clear the screen. No problem. INPUT "Please enter your surname", n$ This asks for your surname and puts it in n$. INPUT "Please enter your first name", f$ This asks for your first name and puts it in f$. INPUT "Please enter Mr/Mrs/Miss/Ms", t$ This asks how to address you and puts it in t$. n = INT(RND * 10) + 1 This puts a random number in n - I'll explain this more thoroughly at a later date. For now, feel free to have a go at messing around with the INT and RND commands to see what they do. p$ = t$ + f$ + n$ + " your number is:" + STR$(n) This is the bit that does the work. It states that p$ should be made to equal t$ plus f$ plus n$ plus the text " your number is:" plus the number in n converted to a string. PRINT p$ This command merely prints out p$ onto the screen. Okay, that wasn't too hard. Have a go at using the other commands to do things like displaying the middle part of a name, displaying a name backwards or any other thing you feel like having a got at. The next lesson will cover IF statements and FOR...NEXT loops. Both of these are essential to programming in basic, and when you've learned them you will be able to create many more useful programs. Until then...