Keywords: LEN, LEFT$, RIGHT$, MID$
Did you have any luck with your alphabetizing program? If not, don't despair; we will go through the development of the program in this chapter. Along the way, we will show you another trick or two. Let's get right to it.
To start off, we want our program to clear any clutter off the screen:
We will then ask the user for the number of words they will want to sort:
PRINT"How many words to sort"; INPUT NUMBER
Wait a minute! Have we forgotten something? If we are going to store and sort some variables, won't we need to dimension a subscripted variable? Yes. Here is our first trick! We will only set aside as much variable space as we need! How do we do that? QBASIC allows us to use a variable as the limit indicator in a dimension statement. Here is how our DIM statement will look:
Now we simply have to enter in all of our data. We will use a method similar to the test scores program in last chapter, that is, printing out the number of each word as it is entered. Here we go:
FOR N=1 TO NUMBER PRINT "Enter Word #"N; INPUT WORD$(N) NEXT N
Now we have all of our words entered into the subscripted variable, and we are using memory in the most efficient way possible at the same time! Incidently, if you enter a number of words that exceeds the available memory of the computer, you will get an Out of Memory message. Use a smaller number of words! We are not trying to store the entire dictionary here! The actual number at which the error message occurs is determined by many factors, including how QBASIC was loaded, and any other programs that are taking up memory. It is not our intention to explain the memory management capabilities of QBASIC, so don't worry about it.
Back to our program. Now that we have the words in our array, we simply need to sort them. To do this, we will use the exact same algorithm that we have been using to sort numbers. This time, instead of comparing and swapping numbers, we will be comparing and swapping words. Here is our next piece of program code:
FOR OUTER =1 TO NUMBER-1 FOR INNER=OUTER+1 TO NUMBER IF WORD$(INNER)<WORD$(OUTER)THEN SWAP WORD$(INNER),WORD$(OUTER) NEXT INNER NEXT OUTER
Let's stop here and discuss how the computer "sees" words. obviously, it knows that "A" comes before "B". But what happens when it sees "APE" and "BUG"? It simply looks at the first letter, and sees again that "A" comes before "B". Let's change it around. What about "APE" and "ABE"? First, the computer compares the first letter of the two words. If they are the same (as they are here), it goes to the second letter of each word. Here it sees that "B" comes before "P". How about something tricky like "SMITH" and "SMITHE"? The computer actually looks at the first five letters, and sees no difference. When it goes to the sixth letter, it sees that it runs out on one of the words. The computer then knows that the one with fewer letters is less than the one with more letters. In the case of "SMITH" and "SMITH", the computer considers them to be equal. They are, aren't they? Smart computer!
In any case, we may now simply print out our sorted list:
FOR N=1 TO NUMBER PRINT WORD$(N) NEXT N END
The easy way to test the program is to run it, and tell it that you have 26 words. As it asks for them, just type in one letter of the alphabet at a time. To make it easy to scramble the letters, just type them in as they appear on the keyboard:
You will see the sorted list printed out when the program finishes. Well, the first four letters will scroll off the top of the screen, but you get the idea.
What kind of fun commands can we use with string variables? Let's look at just a few of the things that QBASIC will let us do! First, switch into the immediate window. Enter:
LET A$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Now we have A$ which contains the alphabet. To verify that it is in there correctly, type in:
Make sure you get the alphabet back. We know that there are 26 letters in the alphabet, but does the computer know that? We can ask it! Type in:
And the computer gives us the number 26! The LEN(string) function return the length of a string! Type in:
and the computer responds with 7. That's how many letters are in the word "letters". We can also look at parts of the string. Type in:
and the computer will respond with the first five letters of the alphabet! This command will take the leftmost characters of the specified string (in our case, A$), and return the requested number of characters. If we request more characters than there are in the string, we just get the whole thing back. QBASIC will not add any spare characters to it. Now let's type this:
Now we get the last five letters of the alphabet! This works almost identical to the LEFT$ function, except that it takes letters from the right side of the string instead of the left side. What if we wanted something in the middle? Type this in:
The computer responds with a "J". If you were counting, you will discover that this is the tenth letter of the alphabet. Can you figure what the 1 does? Let's find out by changing it:
Notice that the computer responds with "JKL". It tells us how many letters to return. If we ask for more letters than there are remaining, it will give us from the specified letter to the end. If we ask it for a starting position that is greater than the length of the string, the computer will give us a null string for an answer.
Here's a fun thing to try: Let's write a program that has the user type in a word or sentence, and we will print out the letters backwards! First, switch back to the program area, and clear out any program you may have. The first thing our program will do is clear the screen. After that, we will have the user enter the string:
CLS PRINT "Enter the sentence to print out backwards:"; INPUT A$
From here, all we have to do is look at one character at a time, starting from the end, and print it out. Have we seen an easy way to look at one character in the middle of the string? Sure! we used the MID$ function earlier to look at the tenth letter of the alphabet! We aren't stuck using numeric constants in our functions, we can use variables! Getting back to the program, the first thing we need to do is figure out how many characters are in our string. We can do that with the LEN function. In fact we will use this function as our starting loop variable. We will need to step backwards through the string variable, so our STEP will be -1. Our ending point will be the first character in the string. Using this information, our FOR statement will look like this:
FOR LETTER = LEN(A$) TO 1 STEP -1
Now we need to display each character at a time. Don't forget to suppress the automatic carriage return on the print statement!
PRINT MID$(A$,LETTER,1); NEXT LETTER
Since we suppressed the carriage return, and we have completed printing out our backwards sentence, we need to put in a carriage return. Do you remember how?
Now go ahead and run the program using this for input:
!SKROW MARGORP RUOY !SNOITALUTARGNOC
If you typed in everything correctly, you will see a neat little message.
Your project to try before the next chapter is a bit involved. The program will sort a list of names. Ask the user for the number of names to be entered. Then enter the names in first-name-space-last-name order, and then sort them by last name. To make the program easier, you are allowed to print out the names in last name - first name order.
HINT: In our solution, we will convert the name that the user enters into last name - first name order immediately after he types it in. It will be stored in the array last name first. We will also show you an option for printing out the sorted list in first name first order. You may try it on your own first if you are feeling bold!
Good Luck! See you next chapter!
Introduced In This Chapter:
Keywords: LEN(), LEFT$(), RIGHT$(), MID$()
Concepts: Dimensioning subscripted variables with a variable, rules for
comparing strings, disassembling a string variable.