More String Functions
KeyWords: DATE$, TIME$, STR$, VAL
We have a lot of ground to cover in this chapter, so we will dig ourselves right into solving the problem presented at the end of the last chapter! We will start off the program just like we did the last few. We'll clear the screen, ask the user how many items are to be entered, and set up our subscripted variable:
CLS PRINT "Enter the number of names to sort:"; INPUT NUMBER DIM NAM$(NUMBER)
Now we will have the user enter the names using a loop:
FOR N = 1 TO NUMBER PRINT "Enter Name #"; N; INPUT N$
Here is where we run into our first challenge! The person running the program will be entering the data not only in first-name last-name order, but possibly first-name middle-initial last-name order. We want to sort our list by last name! What do we do? First, we need to search our string containing the name for a space one character at a time, starting from the back. We are going to assume that a "Jr." will not be used for any name. Then we will split the name into two parts, using the LEFT$ and RIGHT$ functions. We will then reconstruct the name in last-name first-name order using a feature of QBASIC called Concatenation. This is simply connecting two string variables together to make one longer string variable. Here is an example. You may type them in (in the immediate window), if you wish. Suppose we have a program containing these lines:
LET A$ = "ABCDE" LET B$ = "FGHIJ" LET C$ = A$ + B$ PRINT C$
What do you suppose the result would be? If you went ahead and typed them in (in the immediate window), you know that the answer is "ABCDEFGHIJ". What if we had:
LET C$ = A$ + "SPACE" + B$ PRINT C$
Then the answer would be "ABCDESPACEFGHIJ". Using what we know, and making the code as compact as possible, here is how we converted the name entered to last-name first-name order:
FOR CHARACTER = LEN(N$) TO 1 STEP -1 IF MID$(N$, CHARACTER, 1) = " " THEN GOTO SKIPREST NEXT CHARACTER SKIPREST: LET NAM$(N) = RIGHT$(N$, LEN(N$) - CHARACTER) + " " + LEFT$(N$, CHARACTER - 1)
Notice that we needed to use a nested loop to search for our space character. Also note the adjustments we had to make in the arithmetic concerning character placement to avoid having a space at the end of our new string. We also needed to add a space between last name and first name, since they were not actually entered by the user. At this point in the program, the name has been assigned to the subscripted variable, so we need to finish off the data-entering loop:
At this point, we simply sort the list of names. Remember, they are stored last name first. We will use the exact same method that we used in the last chapter. Here it is:
FOR OUTER = 1 TO NUMBER - 1 FOR INNER = OUTER TO NUMBER IF NAM$(INNER) < NAM$(OUTER) THEN SWAP NAM$(INNER), NAM$(OUTER) NEXT INNER NEXT OUTER
Now we simply have to print out our sorted list:
FOR N = 1 TO NUMBER PRINT NAM$(N) NEXT N END
Simple, wasn't it? It is when you get it down to bits and pieces! What we have in this program is three parts. The first part gets the data into the computer, and does some modifying as we enter it. Next, we sorted the list. Then, we printed out the data.
To print out the data in the form that it was entered in, we need to flop the
name back around. You might think that we do it exactly the same way as when we
entered the data. This would work for the first-name last-name method, but for
first-middle-last, it would put the middle name at the front! In essence, we
need to undo what we did earlier by doing it backwards. In other words, we must
begin searching for the space character from the front of the string! Here are
the lines required to do it:
... FOR OUTER=1 TO NUMBER-1 FOR INNER=OUTER TO NUMBER IF NAM$(INNER)<NAM$(OUTER) THEN SWAP NAM$(INNER), NAM$(OUTER) NEXT INNER NEXT OUTER FOR N=1 TO NUMBER FOR CHAR=1 TO LEN(NAM$(N)) IF MID$(NAM$(N),CHAR,1)=" " THEN GOTO SKIPREST2 NEXT CHAR SKIPREST2: PRINT RIGHT$(NAM$(N),LEN(NAM$(N))-CHAR); " ";LEFT$(NAM$(N),CHAR-1) NEXT N END
Notice that the algorithm is nearly identical to the earlier routine, with the only difference being that we started the search from the front of the string instead of the back. The difference is in the inner FOR...NEXT loop.
On to bigger and better things! First, we will tell you about 2 special string variables. These variables are preassigned. Switch to the Immediate window, and type in:
The computer responds with what it thinks is the current date. If it is incorrect, you may change it by typing in:
LET DATE$ = "03/01/97"
or whatever today's date happens to be. The other variable is similar. Type this in:
The computer responds with what is thinks the current time is. The computer tells time in a 24-hour format. In other words, 2:45 pm is displayed as "14:45:00". As with the date, you can set the computer's time with this command:
LET TIME$ = "19:30:00"
if it is 7:30 pm.
Here are two interesting functions that convert a number to a string and back again. Type this in:
LET X = 6 * 8 LET B$ = "Is The Answer" LET A$ = STR$(X) + " " + B$ PRINT A$
The STR$ function turns a number into a string! Can we go the other way? Can we have a string variable that is a number and convert it to a numeric variable? Sure! Type this in:
LET A$="12345" LET B$ = "16 Is A Perfect Square" LET C$ = "There Are 26 Letters In The Alphabet" LET D$ = "This line contains no numbers" PRINT VAL(A$)
The computer responds with the number 12345, as expected. What about the other three string variables? How do they behave? Let's find out:
This only gives you a 16 for an answer. That's because the computer sees the 16, and then sees the space character. As soon as it sees a non-digit character, it quits looking. On to the next one:
Even though there is a number 26 in the string, the computer gives us a 0 result. That is because the first character in the string is not a digit, so the computer stops looking any farther.
Of course, there are no digits at all here, so we get a zero response again.
Since the next chapter is the last one in the series, here is your last project program to try. We will attempt to incorporate a lot of what we have learned into this program.
Here is the final test: Have the user enter a number between 5 and 20. Generate the requested number of random integers in the range of 1 to 50. The output will consist of three column: The first column will be the unsorted list of random numbers. The second column will be the list of numbers sorted as numeric variables. The third column will be the list of numbers sorted as strings. The computer must emit a beep after each column has been printed. The first column must be printed out as each number is generated. The second column must be printed after sorting them as numbers. The third column will then be printed after sorting them as strings.
HINT: Be sure to brush up on the LOCATE command in chapter ten. After you
sort the numbers numerically, you may convert them to strings without having to
"unsort" them. They'll just get resorted anyway. Good luck with this
review, and we'll see you for our final chapter!
Introduced In This Chapter:
Keywords: DATE$, TIME$, STR$, VAL
Concepts: Converting numeric variables to string
variables, converting string variables to numeric variables.