23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

num = [10,12,19,28,33,38,41,46,48,58]<br />

By the way, there is a slight chance that the old <strong>and</strong> num arrays will rema<strong>in</strong><br />

equal even after num is sorted, namely, if num is already sorted before it is<br />

cloned. But the odds of this occurr<strong>in</strong>g are less than one <strong>in</strong> four million.<br />

3.1.4 Simple Cryptography with Str<strong>in</strong>gs <strong>and</strong> Character<br />

Arrays<br />

One of the primary applications of arrays is the representation of str<strong>in</strong>gs of<br />

characters.That is, str<strong>in</strong>g objects are usually stored <strong>in</strong>ternally as an array of<br />

characters. Even if str<strong>in</strong>gs may be represented <strong>in</strong> some other way, there is a natural<br />

relationship between str<strong>in</strong>gs <strong>and</strong> character arrays—both use <strong>in</strong>dices to refer to their<br />

characters. Because of this relationship, <strong>Java</strong> makes it easy for us to create str<strong>in</strong>g<br />

objects from character arrays <strong>and</strong> vice versa. Specifically, to create an object of<br />

class Str<strong>in</strong>g from a character array A, we simply use the expression,<br />

new Str<strong>in</strong>g(A)<br />

That is, one of the constructors for the Str<strong>in</strong>g class takes a character array as its<br />

argument <strong>and</strong> returns a str<strong>in</strong>g hav<strong>in</strong>g the same characters <strong>in</strong> the same order as the<br />

array. For example, the str<strong>in</strong>g we would construct from the array A = [a, c, a, t] is<br />

acat. Likewise, given a str<strong>in</strong>g S, we can create a character array representation of<br />

S by us<strong>in</strong>g the expression,<br />

S.toCharArray()<br />

That is, the Str<strong>in</strong>g class has a method, toCharArray, which returns an array<br />

(of type char[]) with the same characters as S. For example, if we call<br />

toCharArray on the str<strong>in</strong>g adog, we would get the array B = [a, d, o, g].<br />

The Caesar Cipher<br />

One area where be<strong>in</strong>g able to switch from str<strong>in</strong>g to character array <strong>and</strong> back aga<strong>in</strong><br />

is useful is <strong>in</strong> cryptography, the science of secret messages <strong>and</strong> their applications.<br />

This field studies ways of perform<strong>in</strong>g encryption, which takes a message, called<br />

the pla<strong>in</strong>text, <strong>and</strong> converts it <strong>in</strong>to a scrambled message, called the ciphertext.<br />

Likewise, cryptography also studies correspond<strong>in</strong>g ways of perform<strong>in</strong>g<br />

decryption, which takes a ciphertext <strong>and</strong> turns it back <strong>in</strong>to its orig<strong>in</strong>al pla<strong>in</strong>text.<br />

Arguably the earliest encryption scheme is the Caesar cipher, which is named<br />

after Julius Caesar, who used this scheme to protect important military messages.<br />

(All of Caesar's messages were written <strong>in</strong> Lat<strong>in</strong>, of course, which already makes<br />

them unreadable for most of us!) The Caesar cipher is a simple way to obscure a<br />

message written <strong>in</strong> a language that forms words with an alphabet.<br />

155

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!