>> ROB BOWDEN: Hi, I'm Rob.
And when in Rome, let's program as the Romans do.
So, first thing we need to do is check to make sure the user actually entered
the number that we want to rotate each character by at the command line.
>> So, we check.
Is argc equal to 2?
If it's not, that means the user either didn't enter the number at the
command line or they entered too many things at the command line, in which
case we don't know what to do with the rest of the things.
So, as long as argc was two, we can continue with the rest of the program.
>> Now, remember that argv 1 is always a string.
But we want to treat it as an integer.
So, we use the atoi function that's mentioned p set spec to convert the
string argv 1 to an integer, and we store that in key.
We want to make sure that the integer that was entered isn't negative.
And if it was, we want to print that to the user that they're not allowed
to do that, and return.
>> So, now we have a valid number from the user.
But we need to get a string from the user that we want to encrypt.
So we used to do-while loop and use get string to get this string from the
user, and continue getting string until they actually
enter a valid string.
>> Once we have that string, we can go on to encrypt it.
So, this for loop is doing that encryption.
Notice that we're iterating from i equals 0 t all the way up to i equals
n, where n is defined as the length of our message, where the message is the
string that the user entered.
>> So we're iterating over all characters in the string.
And if a character is uppercase, then we're going to shift it using this
formula, which we'll get to in a second.
And if it was lowercase, then we'll shift it using this is
very similar formula.
And if it was neither an uppercase nor a lowercase letter--for example, it
could be a number or a symbol--then we aren't going to do anything to it.
>> So, what are these formulas?
Well, how do we shift the character A?
We want to treat the character A as the number 0.
And we want to treat the character B as the number 1, and so on.
It's the zeroth and first characters of the alphabet and so on.
>> So here, message i minus A is supposed to give us the numeric place of the
letter in the alphabet.
So if message i is A, this is going to give us 0.
Now, we want to add to that the number that the user wants to
rotate the letter by.
So, if they entered 3, then if message i was A, this will give us 0 and plus
key will give us 3.
>> So now, we are considering the third place in the alphabet.
We'll see why we need to mod by num letters in a second.
But finally, we want to add on to the end A again to shift us back into the
ASCII values for these characters instead of the numerical places in the
alphabet of these characters.
>> So, why do we mod by num letters?
Remember that we want to wrap around from the end of the alphabet to the
beginning of the alphabet.
So if the user had entered Z, then subtracting A from that is going to
give us 25.
And if the key were 3, then we're going to have 28.
But Z should wrap around to ABC, so 28 mod num letters, which is hash defined
as 26 to be expected, is going to give us 2, where C is the second letter of
the alphabet, if you remember that A is the zeroth letter of the alphabet.
>> So that's why we need to mod by num letters.
Now, we need to do the same exact thing for lowercase letters, except
remember that when we're dealing with lowercase letters, they have different
ASCII values.
And so we need to subtract and add on a to shift it down from the lowercase
ASCII values and then back to the original lowercase ASCII values.
>> And finally, that'll give us our encrypted message.
And in the end, we just print out that message, and that's it.
My name is Rob, and was Caesar.