1 00:00:00,000 --> 00:00:11,960 2 00:00:11,960 --> 00:00:13,750 >> ROB BOWDEN: Hi, I'm Rob. 3 00:00:13,750 --> 00:00:17,360 And when in Rome, let's program as the Romans do. 4 00:00:17,360 --> 00:00:22,280 So, first thing we need to do is check to make sure the user actually entered 5 00:00:22,280 --> 00:00:26,670 the number that we want to rotate each character by at the command line. 6 00:00:26,670 --> 00:00:27,740 >> So, we check. 7 00:00:27,740 --> 00:00:30,180 Is argc equal to 2? 8 00:00:30,180 --> 00:00:34,190 If it's not, that means the user either didn't enter the number at the 9 00:00:34,190 --> 00:00:37,770 command line or they entered too many things at the command line, in which 10 00:00:37,770 --> 00:00:40,560 case we don't know what to do with the rest of the things. 11 00:00:40,560 --> 00:00:45,750 So, as long as argc was two, we can continue with the rest of the program. 12 00:00:45,750 --> 00:00:49,610 >> Now, remember that argv 1 is always a string. 13 00:00:49,610 --> 00:00:52,490 But we want to treat it as an integer. 14 00:00:52,490 --> 00:00:57,550 So, we use the atoi function that's mentioned p set spec to convert the 15 00:00:57,550 --> 00:01:02,330 string argv 1 to an integer, and we store that in key. 16 00:01:02,330 --> 00:01:06,050 We want to make sure that the integer that was entered isn't negative. 17 00:01:06,050 --> 00:01:09,490 And if it was, we want to print that to the user that they're not allowed 18 00:01:09,490 --> 00:01:11,980 to do that, and return. 19 00:01:11,980 --> 00:01:15,100 >> So, now we have a valid number from the user. 20 00:01:15,100 --> 00:01:19,780 But we need to get a string from the user that we want to encrypt. 21 00:01:19,780 --> 00:01:25,340 So we used to do-while loop and use get string to get this string from the 22 00:01:25,340 --> 00:01:29,190 user, and continue getting string until they actually 23 00:01:29,190 --> 00:01:31,270 enter a valid string. 24 00:01:31,270 --> 00:01:34,400 >> Once we have that string, we can go on to encrypt it. 25 00:01:34,400 --> 00:01:38,120 So, this for loop is doing that encryption. 26 00:01:38,120 --> 00:01:43,250 Notice that we're iterating from i equals 0 t all the way up to i equals 27 00:01:43,250 --> 00:01:47,790 n, where n is defined as the length of our message, where the message is the 28 00:01:47,790 --> 00:01:50,260 string that the user entered. 29 00:01:50,260 --> 00:01:54,330 >> So we're iterating over all characters in the string. 30 00:01:54,330 --> 00:01:59,740 And if a character is uppercase, then we're going to shift it using this 31 00:01:59,740 --> 00:02:01,860 formula, which we'll get to in a second. 32 00:02:01,860 --> 00:02:05,260 And if it was lowercase, then we'll shift it using this is 33 00:02:05,260 --> 00:02:07,290 very similar formula. 34 00:02:07,290 --> 00:02:10,850 And if it was neither an uppercase nor a lowercase letter--for example, it 35 00:02:10,850 --> 00:02:15,370 could be a number or a symbol--then we aren't going to do anything to it. 36 00:02:15,370 --> 00:02:18,220 >> So, what are these formulas? 37 00:02:18,220 --> 00:02:22,090 Well, how do we shift the character A? 38 00:02:22,090 --> 00:02:27,250 We want to treat the character A as the number 0. 39 00:02:27,250 --> 00:02:30,710 And we want to treat the character B as the number 1, and so on. 40 00:02:30,710 --> 00:02:35,170 It's the zeroth and first characters of the alphabet and so on. 41 00:02:35,170 --> 00:02:42,900 >> So here, message i minus A is supposed to give us the numeric place of the 42 00:02:42,900 --> 00:02:44,400 letter in the alphabet. 43 00:02:44,400 --> 00:02:48,080 So if message i is A, this is going to give us 0. 44 00:02:48,080 --> 00:02:52,880 Now, we want to add to that the number that the user wants to 45 00:02:52,880 --> 00:02:54,840 rotate the letter by. 46 00:02:54,840 --> 00:03:03,280 So, if they entered 3, then if message i was A, this will give us 0 and plus 47 00:03:03,280 --> 00:03:05,400 key will give us 3. 48 00:03:05,400 --> 00:03:09,450 >> So now, we are considering the third place in the alphabet. 49 00:03:09,450 --> 00:03:12,810 We'll see why we need to mod by num letters in a second. 50 00:03:12,810 --> 00:03:18,700 But finally, we want to add on to the end A again to shift us back into the 51 00:03:18,700 --> 00:03:22,020 ASCII values for these characters instead of the numerical places in the 52 00:03:22,020 --> 00:03:24,260 alphabet of these characters. 53 00:03:24,260 --> 00:03:26,580 >> So, why do we mod by num letters? 54 00:03:26,580 --> 00:03:29,960 Remember that we want to wrap around from the end of the alphabet to the 55 00:03:29,960 --> 00:03:31,470 beginning of the alphabet. 56 00:03:31,470 --> 00:03:38,530 So if the user had entered Z, then subtracting A from that is going to 57 00:03:38,530 --> 00:03:40,520 give us 25. 58 00:03:40,520 --> 00:03:44,800 And if the key were 3, then we're going to have 28. 59 00:03:44,800 --> 00:03:53,000 But Z should wrap around to ABC, so 28 mod num letters, which is hash defined 60 00:03:53,000 --> 00:04:01,890 as 26 to be expected, is going to give us 2, where C is the second letter of 61 00:04:01,890 --> 00:04:05,880 the alphabet, if you remember that A is the zeroth letter of the alphabet. 62 00:04:05,880 --> 00:04:09,210 >> So that's why we need to mod by num letters. 63 00:04:09,210 --> 00:04:13,540 Now, we need to do the same exact thing for lowercase letters, except 64 00:04:13,540 --> 00:04:17,240 remember that when we're dealing with lowercase letters, they have different 65 00:04:17,240 --> 00:04:18,579 ASCII values. 66 00:04:18,579 --> 00:04:24,550 And so we need to subtract and add on a to shift it down from the lowercase 67 00:04:24,550 --> 00:04:30,020 ASCII values and then back to the original lowercase ASCII values. 68 00:04:30,020 --> 00:04:33,140 >> And finally, that'll give us our encrypted message. 69 00:04:33,140 --> 00:04:37,780 And in the end, we just print out that message, and that's it. 70 00:04:37,780 --> 00:04:39,875 My name is Rob, and was Caesar. 71 00:04:39,875 --> 00:04:49,856