In this class, we focus on writing programs that manipulate
strings. String-manipulation in programs are incredibly
important, so we will spend some time learning how to write 'em.
The mysterious empty string
What's the length of the string "abc"? Three of course? How
about "ab"? Two! How about "a"? One. Well is there a string
of length zero? Yes! We define an empty string like
this: "". It is a string of no characters —
of length zero. Why would I want a string of nothing? Well
... European mathematicians of long ago couldn't see the value
in having a "number" zero? Why would I want a number for
nothing? Actually, we'll see a very common use of the empty
string below, when we create a variable that's initially set to
the empty string so that we can add on to it later.
A useful program: random password generation
There are two basic string operations in Javascript that are
really important:
- .length — whenever you have a string, you can stick
a .length at the end of it to find out how long it is. So,
for example,
"foo".length is 3.
If z is a variable of type
string, z.length tells you its length.
-
[ ] — you can pick individual characters out of a
string using [ ]'s. The only trick is that the index you
use in the [ ]'s starts at 0, not 1. Thus,
"abcd"[0] → "a",
"abcd"[1] → "b",
"abcd"[2] → "c" and
"abcd"[3] → "d".
Note that the last valid index into a string is its length
minus one!
The more "random" a password is, the more secure it is. Let's
use Javacript's Math.random()
function to generate random passwords of six characters.
Exercise: Modify the program so the user specifies the length of the password.
Exercise: Modify the program so punctuation and
mathematical characters can appear in the password.
An interesting exercise is to generate a truly random 7 or 8
character password and see whether you can remember it for a few
minutes. I think you'll find that we humans don't do a good job
of remembering random strings.
Try this from your command prompt
ssh m159999@rona.cs.usna.edu "pwgen -0 -A 8"
This should generate a pronoucable 8-character password. It
should be much easier to remember!
Manipulating ASCII values in Javascript
Javascript has two string-related functions for dealing with
ASCII values:
- .charCodeAt( i ) — whenever you have a string,
you can stick a .charCodeAt( i ) at the end of it,
where i is an index, starting at zero just like with
[ ]'s. So, for example,
"abcd".charCodeAt(2) →
99, because the ASCII code for c is 99.
- String.fromCharCode( i ) — returns a
string consisting of a single character, namely the one with
ASCII code i. So, for example,
String.fromCharCode(99) → "c"
A very traditional use of functions like these is changing a
string to all lower case. Let's start with something easier. We'll
just replace all upper-case letters with a period. If you look
at the ASCII table, you see that A-Z are consecutive in the
table, as are a-z. That makes it easy to test whether something
is indeed an upper-case letter.
The last piece of the puzzle is to take the
variable code, when we know it refers to an
upper-case letter, and come up with the equivalent lower-case
letter. The key is that the letter a comes
32 places after A in the ASCII table. So adding 32
to the ASCII value of an upper-case letter gives you the ASCII
value of the corresponding lower-case letter. So we finish off
the program by replacing res = res + "."; with:
var lower = String.fromCharCode(code + 32);
res = res + lower;
You should try putting the pieces together to make the finished
program.
Morse Code: -- --- .-. ... .
A more interesting example of what we can do with even our
simple knowledge of strings is shown in the following program,
which converts a string of uppercase letters (no space,
punctuation, etc) into morse code:
This is actually the kind of thing we do all the time with
programs: we take data and simply change the way it is
represented. When we do it for secrecy,
it's cryptography, which we'll talk a lot more about
later. When we do it to save memory space
it's compression — which is what .zip files are
all about.