>> ZAMYLA CHAN: Congratulations on finishing your
first couple of C programs.
I know that your first foray into C syntax can be daunting.
But I assure you, at the end of the course, you'll be able to look at the
first couple of assignments and complete them in minutes.
>> Now that you're getting more familiar with syntax, let's get to Caesar.
In Caesar, the user will submit an integer key as a command line
argument, then enter a plain text message at the prompt.
The program will then encipher the text and print
their ciphertext message.
>> The enciphering for Caesar is quite simple.
Shift each letter, in their plain text, by the key.
As a result, it's also pretty insecure.
But implementing Caesar will introduce us to ASCIIMath and array data
structures.
We'll get to more complex ciphers later.
With a Caesar key of 2, the letter A in plain text would be represented by
the letter C in ciphertext because C is two letters after A. B would be
represented by D and C by E. Towards the end of the alphabet, W is
represented by Y, and X by Z. But Y doesn't have two letters after it, so
the ciphers wraps around the alphabet.
Y in plain text is thus represented by A in ciphertext, and Z by B. It may
help to view the Caesar Cypher like a continuous alphabet wheel.
>> To encipher their text, the user will enter two arguments
into the command line--
./caesar followed by a key.
As always, we can't trust the user completely to enter input that make
sense for our program.
So we'll have to validate their command line input.
>> Instead of using int main void, we're using int main, int argc, string argv.
The integer variable argc represents the number of arguments passed into
the command line.
And argv is an array, or think of it as a list, of the arguments passed in.
>> So for Caesar, how do we validate the user's input?
Well, they should only be entering two command line arguments--
./caesar and a key.
So if argc isn't 2, that means that they either forgot a key and just
entered ./caesar, or they entered multiple keys.
>> If this is the case, then you'll want to print instructions
and quit the program.
They'll need to try again from the command line.
But even if argc is 2, you'll need to check whether they
give you a valid key.
For Caesar, you need an integer.
But argv is an array of strings.
How do you access that key?
>> A quick look at arrays--
data structures that hold multiple values of the same data type.
Entries are zero-indexed, meaning that the first element is the index zero
and the last element is at index size minus 1, where size is the number of
elements in the array.
>> If I declared a new string array mailbox of length 3, visually, it
looks like this.
Three containers for strings , side by side.
To access any element, you type the name of the array and then indicate
the index in square brackets.
Here, I'm assigning a value to each element, just as I would do with any
other string variable.
>> So to access our command line arguments, all we have to do is access
the right element of the argv array.
If the user entered ./blastoff Team Rocket into the terminal, argv 0 would
be ./blastoff.
argv would be Team, and arg2 would be rocket.
>> Now that we can access our key, we still need to make
sure that it's correct.
We need to convert it into an integer.
But we can't just cast like we've done previously.
Luckily, the A to Y function takes care of this for us and even returns 0
if the string can't be converted into an integer.
It's up to you, though, to tell the user why you won't
let the program proceed.
Store the result of A to Y in an integer, and there you have your key.
The next part is simple.
Prompt the user for their plain text, which will be of data type string.
Luckily for us, all user inputted strings are valid.
>> Now that we have all necessary input from the user, it's time for us to
encipher their message.
The concept of Caesar is simple enough to understand.
But how does your computer know which letters come after one another?
>> Here's where the ASCII table comes in.
Every character has an integer number associated with it.
Capital A is 65.
Capital B is 66.
Lowercase a is 97.
Lowercase b is 98.
But characters aren't limited to just alphabetic numbers.
For example, the @ symbol is ASCII number 64.
>> Before dealing with the whole string, let's pretend we just have to shift
one character.
Well, we only want to shift actual letters in the plain text, not
characters or numbers.
So the first thing that we'll want to check is whether the character is in
the alphabet.
>> The function isalpha does this for us and returns a Boolean--
true if the characters is a letter, false if otherwise.
Two other useful functions are isupper and islower, with
self-explanatory names.
They return true if the given character is uppercase or lowercase,
respectively.
Since they are Booleans, they're useful to use as conditions.
>> If isalpha returns true, you'll need to shift that character by the key.
So let's open to ASCIIMath and do some ASCII math.
The usage is very similar to the usage for Caesar and takes in a key at the
command line.
>> If I run ASCIIMath 5, it seems to add 5 to a, giving me the letter f, and
displaying the ASCII value.
So let's take a look at the program.
>> You might wonder, right here, why letter is an integer, when it's
clearly, well, a letter.
It turns out that characters and integers are interchangeable.
By putting the letter A in single quotation marks, the integer can store
the ASCII value of capital A. Be careful, though.
You need the single clothes.
Without the single quote marks, the compiler would look for a variable
named A, and not the character.
>> Then I add letter and a key, storing the sum in the int variables result.
Even though result is of data type integer, my printf statement uses the
%c placeholder for characters.
So the program prints the character associated with the integer result.
And since we printed the integer form as well using %d, we see
the number as well.
So now you can see that we treat characters and
integers, and vice versa.
>> Let's test out ASCIIMath a few more times using 25 as a key.
We get the letter z.
Now we try 26.
We want to get the letter a, but instead we get a left bracket.
So obviously, just adding the key to the letter won't do.
We need to figure out a formula to wrap around the alphabet, like our
example in the beginning did.
>> A formula for the Caesar's shift is as follows.
c equals p plus k modulo 26.
Remember that modulo is a useful operation that gives us the remainder
of dividing one number by the other.
Let's apply this formula to the plain text letter with A key of 2.
The ASCII value of y is 89, which gives us 91 modulo 26,
which equals 13--
definitely not the ASCII value of a, which is 67.
>> Humor me now and move away from the ASCII values to an alphabetical index
where A is zero and Z is 25, meaning that Y is 24.
24 plus 2, modulo 6, gives us 26, modulo 26, 0, which is the
alphabetical index of a.
So this formula seems to apply to the alphabetical index of the letter and
not its ASCII value.
>> But you start with ASCII values.
And to print the ciphertext character, you'll need its ASCII value as well.
It's up to you, then, to figure out how to switch back and forth.
>> Once you figure out the right formula for one character, all you need to do
is apply the same formula to every letter in the plain text--
only if that letter is alphabetical, of course.
And remember that you need to preserve the case, upper or lower, that's where
the isUpper and isLower functions mentioned earlier will come in handy.
You might have two formulae--
one for uppercase letters and one for lowercase.
So isUpper an isLower will help you determine which formula to apply.
>> How do you apply your formula to every single character in a string?
Well, a string is just an array of characters.
So you can access each character by grouping over every character in the
string in a for loop.
As for the condition of your for loop, the function strlen, for string
length, will come in handy.
It takes in a string as input and returns the length of that string.
Make sure to include the right library to use the string length function.
>> And there you have your ciphertext.
My name is the Zamyla.
And [SPEAKING CODE].