1 00:00:00,000 --> 00:00:10,103 2 00:00:10,103 --> 00:00:11,270 >> ZAMYLA CHAN: Congratulations on finishing your 3 00:00:11,270 --> 00:00:13,200 first couple of C programs. 4 00:00:13,200 --> 00:00:16,379 I know that your first foray into C syntax can be daunting. 5 00:00:16,379 --> 00:00:20,060 But I assure you, at the end of the course, you'll be able to look at the 6 00:00:20,060 --> 00:00:23,870 first couple of assignments and complete them in minutes. 7 00:00:23,870 --> 00:00:27,830 >> Now that you're getting more familiar with syntax, let's get to Caesar. 8 00:00:27,830 --> 00:00:31,720 In Caesar, the user will submit an integer key as a command line 9 00:00:31,720 --> 00:00:35,300 argument, then enter a plain text message at the prompt. 10 00:00:35,300 --> 00:00:38,050 The program will then encipher the text and print 11 00:00:38,050 --> 00:00:40,020 their ciphertext message. 12 00:00:40,020 --> 00:00:42,980 >> The enciphering for Caesar is quite simple. 13 00:00:42,980 --> 00:00:46,455 Shift each letter, in their plain text, by the key. 14 00:00:46,455 --> 00:00:49,220 As a result, it's also pretty insecure. 15 00:00:49,220 --> 00:00:53,850 But implementing Caesar will introduce us to ASCIIMath and array data 16 00:00:53,850 --> 00:00:54,460 structures. 17 00:00:54,460 --> 00:00:57,510 We'll get to more complex ciphers later. 18 00:00:57,510 --> 00:01:01,680 With a Caesar key of 2, the letter A in plain text would be represented by 19 00:01:01,680 --> 00:01:07,580 the letter C in ciphertext because C is two letters after A. B would be 20 00:01:07,580 --> 00:01:12,450 represented by D and C by E. Towards the end of the alphabet, W is 21 00:01:12,450 --> 00:01:18,550 represented by Y, and X by Z. But Y doesn't have two letters after it, so 22 00:01:18,550 --> 00:01:21,070 the ciphers wraps around the alphabet. 23 00:01:21,070 --> 00:01:27,190 Y in plain text is thus represented by A in ciphertext, and Z by B. It may 24 00:01:27,190 --> 00:01:32,080 help to view the Caesar Cypher like a continuous alphabet wheel. 25 00:01:32,080 --> 00:01:35,760 >> To encipher their text, the user will enter two arguments 26 00:01:35,760 --> 00:01:37,090 into the command line-- 27 00:01:37,090 --> 00:01:40,010 ./caesar followed by a key. 28 00:01:40,010 --> 00:01:44,710 As always, we can't trust the user completely to enter input that make 29 00:01:44,710 --> 00:01:45,800 sense for our program. 30 00:01:45,800 --> 00:01:50,670 So we'll have to validate their command line input. 31 00:01:50,670 --> 00:01:57,285 >> Instead of using int main void, we're using int main, int argc, string argv. 32 00:01:57,285 --> 00:02:01,730 The integer variable argc represents the number of arguments passed into 33 00:02:01,730 --> 00:02:02,880 the command line. 34 00:02:02,880 --> 00:02:09,070 And argv is an array, or think of it as a list, of the arguments passed in. 35 00:02:09,070 --> 00:02:12,000 >> So for Caesar, how do we validate the user's input? 36 00:02:12,000 --> 00:02:15,870 Well, they should only be entering two command line arguments-- 37 00:02:15,870 --> 00:02:18,150 ./caesar and a key. 38 00:02:18,150 --> 00:02:22,340 So if argc isn't 2, that means that they either forgot a key and just 39 00:02:22,340 --> 00:02:27,230 entered ./caesar, or they entered multiple keys. 40 00:02:27,230 --> 00:02:29,770 >> If this is the case, then you'll want to print instructions 41 00:02:29,770 --> 00:02:30,910 and quit the program. 42 00:02:30,910 --> 00:02:34,320 They'll need to try again from the command line. 43 00:02:34,320 --> 00:02:37,430 But even if argc is 2, you'll need to check whether they 44 00:02:37,430 --> 00:02:39,100 give you a valid key. 45 00:02:39,100 --> 00:02:40,730 For Caesar, you need an integer. 46 00:02:40,730 --> 00:02:43,260 But argv is an array of strings. 47 00:02:43,260 --> 00:02:46,490 How do you access that key? 48 00:02:46,490 --> 00:02:47,850 >> A quick look at arrays-- 49 00:02:47,850 --> 00:02:51,410 data structures that hold multiple values of the same data type. 50 00:02:51,410 --> 00:02:55,350 Entries are zero-indexed, meaning that the first element is the index zero 51 00:02:55,350 --> 00:03:00,260 and the last element is at index size minus 1, where size is the number of 52 00:03:00,260 --> 00:03:02,850 elements in the array. 53 00:03:02,850 --> 00:03:07,380 >> If I declared a new string array mailbox of length 3, visually, it 54 00:03:07,380 --> 00:03:08,570 looks like this. 55 00:03:08,570 --> 00:03:11,520 Three containers for strings , side by side. 56 00:03:11,520 --> 00:03:15,445 To access any element, you type the name of the array and then indicate 57 00:03:15,445 --> 00:03:18,080 the index in square brackets. 58 00:03:18,080 --> 00:03:21,610 Here, I'm assigning a value to each element, just as I would do with any 59 00:03:21,610 --> 00:03:24,310 other string variable. 60 00:03:24,310 --> 00:03:29,020 >> So to access our command line arguments, all we have to do is access 61 00:03:29,020 --> 00:03:31,690 the right element of the argv array. 62 00:03:31,690 --> 00:03:37,360 If the user entered ./blastoff Team Rocket into the terminal, argv 0 would 63 00:03:37,360 --> 00:03:38,950 be ./blastoff. 64 00:03:38,950 --> 00:03:45,010 argv would be Team, and arg2 would be rocket. 65 00:03:45,010 --> 00:03:47,670 >> Now that we can access our key, we still need to make 66 00:03:47,670 --> 00:03:49,040 sure that it's correct. 67 00:03:49,040 --> 00:03:51,060 We need to convert it into an integer. 68 00:03:51,060 --> 00:03:54,680 But we can't just cast like we've done previously. 69 00:03:54,680 --> 00:03:58,800 Luckily, the A to Y function takes care of this for us and even returns 0 70 00:03:58,800 --> 00:04:02,110 if the string can't be converted into an integer. 71 00:04:02,110 --> 00:04:04,450 It's up to you, though, to tell the user why you won't 72 00:04:04,450 --> 00:04:06,220 let the program proceed. 73 00:04:06,220 --> 00:04:10,710 Store the result of A to Y in an integer, and there you have your key. 74 00:04:10,710 --> 00:04:12,070 The next part is simple. 75 00:04:12,070 --> 00:04:15,940 Prompt the user for their plain text, which will be of data type string. 76 00:04:15,940 --> 00:04:18,339 Luckily for us, all user inputted strings are valid. 77 00:04:18,339 --> 00:04:21,170 78 00:04:21,170 --> 00:04:24,760 >> Now that we have all necessary input from the user, it's time for us to 79 00:04:24,760 --> 00:04:26,520 encipher their message. 80 00:04:26,520 --> 00:04:29,200 The concept of Caesar is simple enough to understand. 81 00:04:29,200 --> 00:04:33,750 But how does your computer know which letters come after one another? 82 00:04:33,750 --> 00:04:36,100 >> Here's where the ASCII table comes in. 83 00:04:36,100 --> 00:04:39,420 Every character has an integer number associated with it. 84 00:04:39,420 --> 00:04:41,380 Capital A is 65. 85 00:04:41,380 --> 00:04:43,310 Capital B is 66. 86 00:04:43,310 --> 00:04:45,260 Lowercase a is 97. 87 00:04:45,260 --> 00:04:47,590 Lowercase b is 98. 88 00:04:47,590 --> 00:04:50,770 But characters aren't limited to just alphabetic numbers. 89 00:04:50,770 --> 00:04:56,020 For example, the @ symbol is ASCII number 64. 90 00:04:56,020 --> 00:04:59,690 >> Before dealing with the whole string, let's pretend we just have to shift 91 00:04:59,690 --> 00:05:01,220 one character. 92 00:05:01,220 --> 00:05:04,640 Well, we only want to shift actual letters in the plain text, not 93 00:05:04,640 --> 00:05:06,020 characters or numbers. 94 00:05:06,020 --> 00:05:09,100 So the first thing that we'll want to check is whether the character is in 95 00:05:09,100 --> 00:05:10,430 the alphabet. 96 00:05:10,430 --> 00:05:14,460 >> The function isalpha does this for us and returns a Boolean-- 97 00:05:14,460 --> 00:05:18,570 true if the characters is a letter, false if otherwise. 98 00:05:18,570 --> 00:05:22,270 Two other useful functions are isupper and islower, with 99 00:05:22,270 --> 00:05:23,860 self-explanatory names. 100 00:05:23,860 --> 00:05:27,370 They return true if the given character is uppercase or lowercase, 101 00:05:27,370 --> 00:05:28,740 respectively. 102 00:05:28,740 --> 00:05:33,770 Since they are Booleans, they're useful to use as conditions. 103 00:05:33,770 --> 00:05:38,310 >> If isalpha returns true, you'll need to shift that character by the key. 104 00:05:38,310 --> 00:05:43,750 So let's open to ASCIIMath and do some ASCII math. 105 00:05:43,750 --> 00:05:48,700 The usage is very similar to the usage for Caesar and takes in a key at the 106 00:05:48,700 --> 00:05:50,870 command line. 107 00:05:50,870 --> 00:05:59,590 >> If I run ASCIIMath 5, it seems to add 5 to a, giving me the letter f, and 108 00:05:59,590 --> 00:06:01,260 displaying the ASCII value. 109 00:06:01,260 --> 00:06:04,090 So let's take a look at the program. 110 00:06:04,090 --> 00:06:11,820 >> You might wonder, right here, why letter is an integer, when it's 111 00:06:11,820 --> 00:06:14,330 clearly, well, a letter. 112 00:06:14,330 --> 00:06:17,690 It turns out that characters and integers are interchangeable. 113 00:06:17,690 --> 00:06:21,730 By putting the letter A in single quotation marks, the integer can store 114 00:06:21,730 --> 00:06:25,390 the ASCII value of capital A. Be careful, though. 115 00:06:25,390 --> 00:06:27,150 You need the single clothes. 116 00:06:27,150 --> 00:06:31,260 Without the single quote marks, the compiler would look for a variable 117 00:06:31,260 --> 00:06:35,510 named A, and not the character. 118 00:06:35,510 --> 00:06:42,140 >> Then I add letter and a key, storing the sum in the int variables result. 119 00:06:42,140 --> 00:06:47,740 Even though result is of data type integer, my printf statement uses the 120 00:06:47,740 --> 00:06:50,370 %c placeholder for characters. 121 00:06:50,370 --> 00:06:54,530 So the program prints the character associated with the integer result. 122 00:06:54,530 --> 00:07:00,400 And since we printed the integer form as well using %d, we see 123 00:07:00,400 --> 00:07:02,110 the number as well. 124 00:07:02,110 --> 00:07:04,450 So now you can see that we treat characters and 125 00:07:04,450 --> 00:07:06,980 integers, and vice versa. 126 00:07:06,980 --> 00:07:12,205 >> Let's test out ASCIIMath a few more times using 25 as a key. 127 00:07:12,205 --> 00:07:15,510 128 00:07:15,510 --> 00:07:17,090 We get the letter z. 129 00:07:17,090 --> 00:07:19,750 Now we try 26. 130 00:07:19,750 --> 00:07:25,600 We want to get the letter a, but instead we get a left bracket. 131 00:07:25,600 --> 00:07:29,490 So obviously, just adding the key to the letter won't do. 132 00:07:29,490 --> 00:07:32,780 We need to figure out a formula to wrap around the alphabet, like our 133 00:07:32,780 --> 00:07:34,570 example in the beginning did. 134 00:07:34,570 --> 00:07:38,520 >> A formula for the Caesar's shift is as follows. 135 00:07:38,520 --> 00:07:42,750 c equals p plus k modulo 26. 136 00:07:42,750 --> 00:07:46,040 Remember that modulo is a useful operation that gives us the remainder 137 00:07:46,040 --> 00:07:49,880 of dividing one number by the other. 138 00:07:49,880 --> 00:07:54,870 Let's apply this formula to the plain text letter with A key of 2. 139 00:07:54,870 --> 00:08:01,810 The ASCII value of y is 89, which gives us 91 modulo 26, 140 00:08:01,810 --> 00:08:03,690 which equals 13-- 141 00:08:03,690 --> 00:08:08,740 definitely not the ASCII value of a, which is 67. 142 00:08:08,740 --> 00:08:12,810 >> Humor me now and move away from the ASCII values to an alphabetical index 143 00:08:12,810 --> 00:08:18,690 where A is zero and Z is 25, meaning that Y is 24. 144 00:08:18,690 --> 00:08:25,830 24 plus 2, modulo 6, gives us 26, modulo 26, 0, which is the 145 00:08:25,830 --> 00:08:28,170 alphabetical index of a. 146 00:08:28,170 --> 00:08:32,980 So this formula seems to apply to the alphabetical index of the letter and 147 00:08:32,980 --> 00:08:34,960 not its ASCII value. 148 00:08:34,960 --> 00:08:37,630 >> But you start with ASCII values. 149 00:08:37,630 --> 00:08:41,650 And to print the ciphertext character, you'll need its ASCII value as well. 150 00:08:41,650 --> 00:08:46,400 It's up to you, then, to figure out how to switch back and forth. 151 00:08:46,400 --> 00:08:49,850 >> Once you figure out the right formula for one character, all you need to do 152 00:08:49,850 --> 00:08:53,520 is apply the same formula to every letter in the plain text-- 153 00:08:53,520 --> 00:08:57,720 only if that letter is alphabetical, of course. 154 00:08:57,720 --> 00:09:02,360 And remember that you need to preserve the case, upper or lower, that's where 155 00:09:02,360 --> 00:09:06,890 the isUpper and isLower functions mentioned earlier will come in handy. 156 00:09:06,890 --> 00:09:08,830 You might have two formulae-- 157 00:09:08,830 --> 00:09:11,680 one for uppercase letters and one for lowercase. 158 00:09:11,680 --> 00:09:18,420 So isUpper an isLower will help you determine which formula to apply. 159 00:09:18,420 --> 00:09:22,460 >> How do you apply your formula to every single character in a string? 160 00:09:22,460 --> 00:09:25,910 Well, a string is just an array of characters. 161 00:09:25,910 --> 00:09:31,150 So you can access each character by grouping over every character in the 162 00:09:31,150 --> 00:09:33,450 string in a for loop. 163 00:09:33,450 --> 00:09:37,550 As for the condition of your for loop, the function strlen, for string 164 00:09:37,550 --> 00:09:39,280 length, will come in handy. 165 00:09:39,280 --> 00:09:44,020 It takes in a string as input and returns the length of that string. 166 00:09:44,020 --> 00:09:49,250 Make sure to include the right library to use the string length function. 167 00:09:49,250 --> 00:09:51,790 >> And there you have your ciphertext. 168 00:09:51,790 --> 00:09:53,260 My name is the Zamyla. 169 00:09:53,260 --> 00:09:54,510 And [SPEAKING CODE]. 170 00:09:54,510 --> 00:10:02,944