336 Part II Programming Fundamentals
For i = span To numOfElements - 1
For j = (i - span) To 0 Step -span
If sort(j) <= sort(j + span) Then Exit For
'swap array elements that are out of order
temp = sort(j)
sort(j) = sort(j + span)
sort(j + span) = temp
Next j
Next i
span = span \ 2
Loop
End Sub
The method of the sort is to continually divide the main list of elements into sublists
that are smaller by half. The sort then compares the tops and the bottoms of the
sublists to see whether the elements are out of order. If the top and bottom are out of
order, they’re exchanged. The result is an array named sort() that’s sorted alphabetically
in descending order. To change the direction of the sort, simply reverse the relational
operator (change <= to >=).
The remaining event procedures in Form1 (OpenToolStripMenuItem_Click,
CloseToolStripMenuItem_Click, SaveAsToolStripMenuItem_Click, InsertDateToolStripMenuItem_
Click, and ExitToolStripMenuItem_Click) are all similar to the procedures that you studied in the
Text Browser and the Quick Note programs. (See my explanations earlier in this chapter for the
details .)
Let’s move on to another variation of this program that manipulates the strings in a text box
or a file.
Protecting Text with Basic Encryption
Now that you’ve had some experience with ASCII codes, you can begin to write simple
encryption routines that shift the ASCII codes in your documents and “scramble” the text
to hide it from intruding eyes. This process, known as encryption, mathematically alters
the characters in a file, making them unreadable to the casual observer. Of course, to
use encryption successfully, you also need to be able to reverse the process— otherwise,
you’ll simply be trashing your files rather than protecting them. And you’ll want to create
an encryption scheme or key that can’t be easily recognized, a complicated process
that’s only begun by the sample programs in this chapter.
The following exercises show you how to encrypt and decrypt text strings safely. You’ll
run the Encrypt Text program now to see a simple encryption scheme in action. As I note at
the end of this chapter, these exercises are just the tip of the iceberg for using encryption,
cryptography, and file security measures—and these issues have become major areas
of interest for programmers in the last decade or so. Still, even basic encryption is fun
and a useful demonstration of text-processing techniques.