1 00:00:13,196 --> 00:00:13,966 >> Hello, everybody. 2 00:00:13,966 --> 00:00:15,846 Welcome to walkthrough 2. 3 00:00:16,166 --> 00:00:19,666 As we begin our third pset in cs50, this week focusing 4 00:00:19,666 --> 00:00:21,866 on cryptography; so taking some message 5 00:00:21,866 --> 00:00:23,746 that someone else can read and encoding it 6 00:00:23,746 --> 00:00:26,646 so that you can't read it unless you know the keyword. 7 00:00:26,756 --> 00:00:30,386 So this was today's music; a band called Rehab. 8 00:00:30,386 --> 00:00:31,626 I like them very much. 9 00:00:32,096 --> 00:00:36,786 So as the pset says several times, before you do anything 10 00:00:36,786 --> 00:00:38,226 with this pset, before you try to submit, 11 00:00:38,596 --> 00:00:39,866 please update your appliance. 12 00:00:40,026 --> 00:00:42,866 We've released a new version of our submit50 program 13 00:00:42,866 --> 00:00:44,506 that hopefully addresses some of the issues 14 00:00:44,506 --> 00:00:45,496 that some people ran into. 15 00:00:45,766 --> 00:00:47,916 So to get that new version you just have to say 16 00:00:47,916 --> 00:00:49,886 "sudo yum, dash, y update." 17 00:00:50,026 --> 00:00:51,506 That's going to pull down a bunch of updates. 18 00:00:51,566 --> 00:00:53,386 Please don't close your laptop or anything 19 00:00:53,386 --> 00:00:54,786 like that while this is happening. 20 00:00:55,336 --> 00:00:59,076 If you get a message saying it can't find the meta link 21 00:00:59,076 --> 00:01:00,256 or something like that, that just means 22 00:01:00,256 --> 00:01:01,866 that your Internet's not connected. 23 00:01:01,866 --> 00:01:04,066 Check out "manual, dot, cs 50, dot, net, slash, 24 00:01:04,066 --> 00:01:07,026 faqs" for some instructions on how to get that working again. 25 00:01:07,026 --> 00:01:09,436 Just a reminder, it's going to ask you for your password, 26 00:01:09,686 --> 00:01:12,156 which is just J Harvard's password which is "crimson" 27 00:01:12,296 --> 00:01:14,526 and you're not going to see it as you're typing it just 28 00:01:14,526 --> 00:01:15,686 for security purposes. 29 00:01:15,726 --> 00:01:18,566 And same thing with submit50, when you enter 30 00:01:18,566 --> 00:01:20,546 in your cloud user name and password, you're not going 31 00:01:20,546 --> 00:01:21,926 to see your password as you type it. 32 00:01:21,926 --> 00:01:23,876 So don't worry, the terminal's not frozen, 33 00:01:23,876 --> 00:01:25,126 you're just not seeing what you're typing 34 00:01:25,126 --> 00:01:26,306 for security purposes. 35 00:01:26,936 --> 00:01:27,936 So this -- yup? 36 00:01:28,016 --> 00:01:28,726 >> Oh, sorry. 37 00:01:28,726 --> 00:01:32,786 How long ago was that updated to the website 38 00:01:32,846 --> 00:01:36,316 because I think I did that before I submitted my last pset? 39 00:01:36,486 --> 00:01:39,406 >> Oh, so how long ago was this -- the yum update ready to go? 40 00:01:39,656 --> 00:01:41,616 So we're just going to be -- keep releasing these basically, 41 00:01:41,616 --> 00:01:43,046 so it's always good to just make sure you have the 42 00:01:43,046 --> 00:01:43,846 latest versions. 43 00:01:43,846 --> 00:01:45,896 Yeah, so anytime we -- someone runs into a problem, 44 00:01:46,006 --> 00:01:47,456 we're going to fix it and we're going to release it 45 00:01:47,456 --> 00:01:49,946 and then you can get the new version by simply doing this. 46 00:01:50,096 --> 00:01:50,316 >> All right. 47 00:01:50,316 --> 00:01:51,736 >> Doing this when there's no new version won't hurt 48 00:01:51,736 --> 00:01:52,246 your computer. 49 00:01:52,246 --> 00:01:53,446 It will just take two seconds/, it will say, 50 00:01:53,446 --> 00:01:54,476 "no new updates available." 51 00:01:54,696 --> 00:01:55,246 You're ready to go. 52 00:01:55,546 --> 00:01:57,036 So do this as much as possible. 53 00:01:57,036 --> 00:02:01,326 It won't hurt/ other questions with submission problems? 54 00:02:01,856 --> 00:02:02,886 Okay, cool. 55 00:02:03,096 --> 00:02:04,486 That's good to hear. 56 00:02:05,596 --> 00:02:10,056 So this week it's also good to focus on backing up your code. 57 00:02:10,236 --> 00:02:11,866 So when you run the submit50, 58 00:02:11,866 --> 00:02:14,366 you're technically submitting your code; however, 59 00:02:14,366 --> 00:02:15,706 submitting your code on a Tuesday 60 00:02:15,706 --> 00:02:17,166 and then submitting again on a Wednesday, 61 00:02:17,166 --> 00:02:18,876 there's nothing really wrong with that. 62 00:02:18,876 --> 00:02:20,856 And what happens when you submit it is you're sending it 63 00:02:20,856 --> 00:02:22,326 over to the CS50 server. 64 00:02:22,326 --> 00:02:25,636 So if you drop your laptop or it goes swimming or something 65 00:02:25,706 --> 00:02:27,286 and you lose all your code, there's not to worry 66 00:02:27,286 --> 00:02:29,426 because you've backed it up on the CS50 servers 67 00:02:29,426 --> 00:02:31,666 and we can get you a copy back really simply. 68 00:02:32,166 --> 00:02:34,486 Now, there's also Dropbox integrated to the appliance. 69 00:02:34,486 --> 00:02:35,316 So you might have heard of this. 70 00:02:35,316 --> 00:02:36,196 It's a little program 71 00:02:36,546 --> 00:02:39,816 that basically synchronizes some folder with the Dropbox server; 72 00:02:39,816 --> 00:02:41,126 so it will just run in the background, 73 00:02:41,376 --> 00:02:43,756 and every time you make a change to a file or create a new file 74 00:02:43,756 --> 00:02:45,586 and put it inside of that Dropbox folder, 75 00:02:46,156 --> 00:02:48,866 it's going to synchronize it and back it up so you'll never have 76 00:02:48,866 --> 00:02:50,186 to worry about losing those files. 77 00:02:50,746 --> 00:02:53,056 So I'd encourage you to try out both of these options as a means 78 00:02:53,056 --> 00:02:54,166 of backing up your code. 79 00:02:54,706 --> 00:02:58,026 And finally, we can also get code off the appliance 80 00:02:58,026 --> 00:02:58,616 really easily. 81 00:02:58,956 --> 00:03:01,176 So one way to do this is to just to e-mail a bunch of files 82 00:03:01,176 --> 00:03:03,056 to yourself but that gets kind 83 00:03:03,056 --> 00:03:04,446 of tedious once you have a lot of files. 84 00:03:05,136 --> 00:03:07,566 And so here are two ways documented 85 00:03:07,826 --> 00:03:10,586 on manual.cs50.net/appliance again. 86 00:03:11,006 --> 00:03:13,106 But really basically what you can do by simply entering 87 00:03:13,106 --> 00:03:17,156 in these two simple commands is create a folder on your desktop 88 00:03:17,156 --> 00:03:18,366 that is The Appliance. 89 00:03:18,426 --> 00:03:21,266 So it will look like any other folder on your computer 90 00:03:21,266 --> 00:03:23,896 but you'll actually be accessing the files inside 91 00:03:23,896 --> 00:03:26,336 of your appliance, so you can copy things back and forth. 92 00:03:27,646 --> 00:03:30,006 So questions on some little appliance tricks? 93 00:03:30,506 --> 00:03:33,146 Happy to show you more if you're curious. 94 00:03:34,236 --> 00:03:37,536 So the first problem on this problem set is called "Old Man." 95 00:03:38,246 --> 00:03:40,396 So what we need to do here is produce output 96 00:03:40,396 --> 00:03:43,036 like this except ten more verses of this, 97 00:03:43,286 --> 00:03:45,816 basically outputting the song "This Old Man." 98 00:03:46,266 --> 00:03:49,556 And so our to do list here isn't too bad. 99 00:03:50,076 --> 00:03:52,556 So we basically need to do two things: We need to loop 100 00:03:52,556 --> 00:03:55,006 over our verses, right, because we have ten verses 101 00:03:55,006 --> 00:03:56,176 of which they're all pretty similar, 102 00:03:56,426 --> 00:03:58,716 so feels like the loop would be a good idea to leverage here; 103 00:03:58,716 --> 00:04:01,126 and then as we're looping over them we want 104 00:04:01,126 --> 00:04:03,136 to display the entire verse. 105 00:04:04,156 --> 00:04:06,346 So there are several different approaches we can take here. 106 00:04:06,906 --> 00:04:09,406 So as I said, each of the verses is only slightly different 107 00:04:10,106 --> 00:04:11,596 so there's a few things we could do. 108 00:04:11,826 --> 00:04:14,666 We could say "store the verses in variables," right. 109 00:04:14,706 --> 00:04:17,096 There's a lot of repeated text so if we save that text 110 00:04:17,096 --> 00:04:18,916 in a variable and output that text 111 00:04:18,966 --> 00:04:21,376 and then only output the different text, 112 00:04:22,626 --> 00:04:23,736 then that's one way to approach it. 113 00:04:24,056 --> 00:04:26,186 Or we can do something like "use conditions," where we can say, 114 00:04:26,186 --> 00:04:28,756 okay, well, if I'm on the first verse, I'm going to say this. 115 00:04:29,316 --> 00:04:30,626 Rather than, if I'm on the first verse, 116 00:04:30,626 --> 00:04:31,796 I'm going to use this variable. 117 00:04:32,276 --> 00:04:33,906 We could also use arrays here. 118 00:04:33,906 --> 00:04:36,156 So we haven't really gotten into arrays too much yet, 119 00:04:36,156 --> 00:04:38,056 but we certainly will in the coming week, 120 00:04:38,166 --> 00:04:40,156 but this is another opportunity to use arrays. 121 00:04:40,156 --> 00:04:42,406 We can put all of our verses in an array, 122 00:04:42,406 --> 00:04:43,976 and then go through that in order 123 00:04:43,976 --> 00:04:46,146 to output the correct song lyrics. 124 00:04:47,106 --> 00:04:48,076 Okay, so that's done. 125 00:04:48,076 --> 00:04:50,406 So now how can we display each verse? 126 00:04:51,546 --> 00:04:54,116 So this is a great opportunity to start using functions. 127 00:04:54,646 --> 00:04:56,756 So up to this point we've basically seen functions 128 00:04:56,756 --> 00:04:58,206 that other people wrote for us. 129 00:04:58,566 --> 00:05:01,026 Like printf is a function, get int is a function, 130 00:05:01,026 --> 00:05:02,956 but we didn't actually write those functions. 131 00:05:03,146 --> 00:05:04,776 Of course, we can write our own functions, 132 00:05:04,896 --> 00:05:06,416 and that's where programming gets really fun. 133 00:05:07,156 --> 00:05:09,336 So a function is basically a block of code aimed 134 00:05:09,336 --> 00:05:11,006 at accomplishing a single task. 135 00:05:11,646 --> 00:05:12,806 So something like get int. 136 00:05:12,976 --> 00:05:15,866 It has the single task of getting something from the user 137 00:05:15,866 --> 00:05:17,806 and giving it back to you in a variable. 138 00:05:18,266 --> 00:05:20,956 So functions can take input in the form of arguments, 139 00:05:21,246 --> 00:05:23,356 so we can say, I want to display the first verse; 140 00:05:23,356 --> 00:05:26,196 I want to display the second verse; and they produce output. 141 00:05:26,196 --> 00:05:28,306 So they can return some value, they can print 142 00:05:28,306 --> 00:05:30,006 out to the screen, they can do whatever you want. 143 00:05:30,006 --> 00:05:32,576 So in this case we have our single task. 144 00:05:32,766 --> 00:05:34,496 We want to display a verse. 145 00:05:34,996 --> 00:05:38,026 So as our input to this function we might want to say which verse 146 00:05:38,026 --> 00:05:39,866 to display, you know, one, two, three, four; 147 00:05:40,266 --> 00:05:42,966 and our output is going to be the text of the verse. 148 00:05:43,016 --> 00:05:44,996 Whether you return that or print that directly 149 00:05:44,996 --> 00:05:46,646 to the screen, is up to you. 150 00:05:48,096 --> 00:05:49,666 So that's it for Old Man. 151 00:05:50,406 --> 00:05:51,746 It's a good warm up exercise. 152 00:05:51,786 --> 00:05:58,386 But any questions on how you might want to approach that? 153 00:05:59,036 --> 00:06:02,016 Okay. So let's get to the more interesting two problems. 154 00:06:02,456 --> 00:06:05,226 So the first of the two ciphers you'll be implementing is called 155 00:06:05,226 --> 00:06:06,136 the Caesar cipher. 156 00:06:06,576 --> 00:06:08,406 So this is what a good output 157 00:06:08,406 --> 00:06:09,686 of your program is going to look like. 158 00:06:10,166 --> 00:06:12,516 You're going to run Caesar and you're going to supply it now 159 00:06:12,516 --> 00:06:14,256 with what's called a command line argument. 160 00:06:14,596 --> 00:06:17,536 So rather than asking the user to type in 13, 161 00:06:17,746 --> 00:06:19,476 we're going to specify some number 162 00:06:19,766 --> 00:06:21,236 when they actually run the program. 163 00:06:22,026 --> 00:06:24,906 Then after that, you're going to prompt the user for some input 164 00:06:24,906 --> 00:06:26,756 or the message that they want to encrypt, 165 00:06:27,026 --> 00:06:30,126 and then you're just going to spit out the encrypted message. 166 00:06:30,686 --> 00:06:33,906 So here's our outline of what we want to do. 167 00:06:34,196 --> 00:06:37,206 We first want to determine what that number is 168 00:06:37,256 --> 00:06:38,976 that the user typed in at the command line. 169 00:06:39,076 --> 00:06:40,346 In this case it was 13. 170 00:06:40,776 --> 00:06:43,846 And so throughout our program and in the pset specification, 171 00:06:43,846 --> 00:06:45,986 this is just our k; k for key. 172 00:06:46,976 --> 00:06:48,676 So after we determine what that is, 173 00:06:48,676 --> 00:06:52,386 we want to ask the user what the message they want to encrypt is. 174 00:06:53,516 --> 00:06:56,236 Then we want to loop over each character of the string 175 00:06:56,596 --> 00:06:59,046 and encrypt it and output it to the user 176 00:06:59,046 --> 00:07:00,866 so we have an encrypted message. 177 00:07:02,176 --> 00:07:04,636 So let's take a look at how we're going to get 178 00:07:04,636 --> 00:07:06,306 that number off the command line. 179 00:07:07,436 --> 00:07:09,676 So you'll notice before that we had inside 180 00:07:09,676 --> 00:07:11,566 of our main declaration -- oh, question first. 181 00:07:11,716 --> 00:07:16,646 >> Sorry. Would you mind going back to the previous slide. 182 00:07:16,756 --> 00:07:16,846 >> Yup. 183 00:07:16,846 --> 00:07:16,913 >> [Inaudible] 184 00:07:16,913 --> 00:07:18,576 >> Sure. So once we have the message, 185 00:07:19,376 --> 00:07:21,146 we want to encrypt that message. 186 00:07:21,146 --> 00:07:23,296 So that means we want to encrypt every letter. 187 00:07:23,926 --> 00:07:26,426 So in order to do that, we want to have some form of loop 188 00:07:26,606 --> 00:07:29,636 that goes over every letter in the string, does something 189 00:07:29,636 --> 00:07:32,816 to it, and then spits back what the encrypted character 190 00:07:32,816 --> 00:07:33,226 should be. 191 00:07:33,676 --> 00:07:35,736 So by doing that, if we do that for every character 192 00:07:35,736 --> 00:07:37,216 in the string, then eventually we're going 193 00:07:37,216 --> 00:07:38,896 to have our totally encrypted string 194 00:07:38,986 --> 00:07:40,516 and we can send it back to the user. 195 00:07:41,036 --> 00:07:46,756 Okay, so before when we use main we just said "int main void." 196 00:07:46,756 --> 00:07:48,726 And that basically said that, okay, 197 00:07:48,726 --> 00:07:50,226 I have this function called main 198 00:07:50,476 --> 00:07:51,926 but it doesn't take any arguments 199 00:07:51,996 --> 00:07:54,146 so I can just say void; however, 200 00:07:54,146 --> 00:07:56,476 the main function can take arguments, 201 00:07:56,786 --> 00:08:00,206 and those arguments come kind of magically from the command line. 202 00:08:01,176 --> 00:08:03,216 So there are two arguments to our main function. 203 00:08:03,396 --> 00:08:06,436 The first is called argc which stands for arg count 204 00:08:06,436 --> 00:08:08,766 or the number of arguments that have been given, 205 00:08:08,766 --> 00:08:10,246 so this will always be a number. 206 00:08:11,286 --> 00:08:15,356 The second of these is argv, so this is an array of strings. 207 00:08:15,876 --> 00:08:18,056 So everything that I've typed at the command line, 208 00:08:18,056 --> 00:08:20,836 including the program itself, is going to be placed 209 00:08:20,836 --> 00:08:22,606 in a block inside of this array. 210 00:08:22,986 --> 00:08:26,706 So if I run caesar 13 at the command, line when I'm inside 211 00:08:26,706 --> 00:08:30,416 of main, I have an argc of 2, because the name 212 00:08:30,416 --> 00:08:31,376 of the program counts; 213 00:08:32,156 --> 00:08:35,956 then I have my first arg value is the string caesar, 214 00:08:35,956 --> 00:08:39,106 and the second is the string 13. 215 00:08:39,666 --> 00:08:45,216 So we don't want to use a string as our key, right? 216 00:08:45,216 --> 00:08:47,766 We don't want to try to move things over by strings, 217 00:08:47,766 --> 00:08:49,096 we want to add numbers. 218 00:08:49,376 --> 00:08:52,466 So the first thing we need to do is convert this string 219 00:08:52,886 --> 00:08:56,366 which is inside of argv to a number that we can use. 220 00:08:57,226 --> 00:09:00,516 So there's a special function that's defined for us called "a 221 00:09:00,516 --> 00:09:04,186 to i," [pronounced] and this takes one string as an argument 222 00:09:04,396 --> 00:09:07,506 and it returns the integer that's enclosed inside 223 00:09:07,506 --> 00:09:08,126 of that string. 224 00:09:08,596 --> 00:09:10,846 Now, if I had something that's not a number inside 225 00:09:10,846 --> 00:09:14,446 of the string, it's just going to return zero, so we don't have 226 00:09:14,446 --> 00:09:16,336 to worry about error checking as the pset says. 227 00:09:16,616 --> 00:09:17,066 Yup? 228 00:09:17,236 --> 00:09:22,306 >> So is atoi automatically included in, like, this CS50 229 00:09:22,306 --> 00:09:23,516 or one of those standard io - 230 00:09:23,996 --> 00:09:25,806 >> Yup. So is atoi automatically included? 231 00:09:25,806 --> 00:09:27,956 Nope. You're going to need to include a separate header file 232 00:09:27,956 --> 00:09:29,206 which we'll see shortly. 233 00:09:29,416 --> 00:09:31,016 So yes. So it's a new one that we're going to need. 234 00:09:32,266 --> 00:09:32,966 Other questions? 235 00:09:33,466 --> 00:09:33,566 Yup? 236 00:09:34,596 --> 00:09:39,186 >> [Inaudible] about a number is [inaudible] return zero? 237 00:09:39,366 --> 00:09:42,506 >> Sure. So in this case we have a number, right, our string a, 238 00:09:42,506 --> 00:09:44,346 it just contains an actual integer. 239 00:09:44,746 --> 00:09:46,946 So in this case it's going to return 50. 240 00:09:47,546 --> 00:09:50,246 Now, if I said something like, string a equals word, 241 00:09:50,246 --> 00:09:52,686 and then I said, atoi on the string, 242 00:09:52,686 --> 00:09:55,426 it's going to return zero because word isn't a number 243 00:09:55,426 --> 00:09:57,126 so atoi's going to say, well, I'm just going 244 00:09:57,126 --> 00:09:59,576 to give you back zero because that's clearly not what 245 00:09:59,576 --> 00:09:59,896 you wanted. 246 00:10:00,426 --> 00:10:00,666 Yup? 247 00:10:01,176 --> 00:10:03,416 >> Does that work for nonletters as well? 248 00:10:03,636 --> 00:10:05,316 Like a comma or something like that? 249 00:10:05,626 --> 00:10:07,296 >> So does this work for nonletters as well? 250 00:10:07,296 --> 00:10:08,726 Yeah. If there's anything inside 251 00:10:08,726 --> 00:10:10,276 of this string that's not a number, 252 00:10:10,276 --> 00:10:11,406 atoi's going to say zero. 253 00:10:12,096 --> 00:10:14,156 So it has to be purely an integer for you 254 00:10:14,156 --> 00:10:15,336 to get back a valid value. 255 00:10:16,266 --> 00:10:16,436 Yup? 256 00:10:16,956 --> 00:10:18,686 >> For argv, how does it distinguish 257 00:10:19,056 --> 00:10:20,736 between two separate strings? 258 00:10:20,736 --> 00:10:22,706 Like, you have the first string and you have the second string, 259 00:10:22,706 --> 00:10:24,516 and I remember you mentioned a lecture 260 00:10:24,516 --> 00:10:27,726 like this something you used to distinguish between the two. 261 00:10:27,876 --> 00:10:30,486 >> Sure. So how does argv kind of break up the arguments 262 00:10:30,486 --> 00:10:32,326 into separate blocks in the array? 263 00:10:32,556 --> 00:10:33,246 So spaces. 264 00:10:33,906 --> 00:10:36,626 So if I said caesar 13, space, word, 265 00:10:36,926 --> 00:10:40,966 then my argv would have three elements in it, maybe caesar 13, 266 00:10:40,966 --> 00:10:42,196 maybe c013 and the string word. 267 00:10:42,506 --> 00:10:44,716 So everything that's separated by a space is going 268 00:10:44,716 --> 00:10:46,346 to be a new element inside of argv. 269 00:10:46,346 --> 00:10:50,376 >> I mean inside actual array though, how does it know that, 270 00:10:50,436 --> 00:10:53,446 like, this -- does it just put arg, program name, 271 00:10:53,976 --> 00:10:55,956 and then what you inputted, like, together in the array? 272 00:10:56,716 --> 00:10:57,776 So then how would it know -- 273 00:10:58,436 --> 00:11:02,836 >> So argv is an array and it has argc number of elements. 274 00:11:03,136 --> 00:11:05,456 So the first element in the array is kind of -- 275 00:11:05,616 --> 00:11:07,446 each element in the array is kind of separate 276 00:11:07,446 --> 00:11:08,356 from the next element. 277 00:11:08,576 --> 00:11:09,716 So this all happens for you. 278 00:11:09,716 --> 00:11:11,086 You don't want to try to determine 279 00:11:11,086 --> 00:11:12,926 like where it's end and don't end. 280 00:11:13,516 --> 00:11:15,716 So it's going to break everything up by spaces, 281 00:11:15,716 --> 00:11:17,866 and then the first thing is going to go in 0; 282 00:11:18,146 --> 00:11:19,956 then the next word, if there's a space, 283 00:11:19,956 --> 00:11:21,566 is going to go into one; and so on. 284 00:11:21,886 --> 00:11:23,886 Yeah. So this is all done for you and we don't really have 285 00:11:23,886 --> 00:11:26,066 to worry about how it happens, we just need to know 286 00:11:26,066 --> 00:11:30,346 that argv has an array of all the strings that were sent along 287 00:11:30,346 --> 00:11:31,396 when we ran the program. 288 00:11:31,936 --> 00:11:38,456 Okay, so let's take a look at an example 289 00:11:38,456 --> 00:11:41,166 of using this argv and argc. 290 00:11:42,126 --> 00:11:52,996 So if we go into the appliance and open up this args.c, 291 00:11:53,906 --> 00:11:56,146 so now instead of saying int main void, 292 00:11:56,146 --> 00:12:01,316 we're now saying int main, int argc, and now string argv 293 00:12:01,316 --> 00:12:04,016 because this is an array of strings. 294 00:12:04,846 --> 00:12:07,246 So these two brackets tell me 295 00:12:07,246 --> 00:12:09,096 that this is not just a single string 296 00:12:09,096 --> 00:12:11,406 but this is actually an array of strings. 297 00:12:11,836 --> 00:12:15,026 So now I can do something like iterate over everything 298 00:12:15,026 --> 00:12:16,676 that was given to me at the command line. 299 00:12:17,166 --> 00:12:18,596 So we're going to use a four loop, 300 00:12:18,596 --> 00:12:22,666 so i is our counter variable, and we know how many things are 301 00:12:22,666 --> 00:12:25,846 in this array because they are argc number 302 00:12:25,846 --> 00:12:26,716 of things in the array. 303 00:12:27,126 --> 00:12:32,136 So I can say, while i is less than argc, I can print out argv, 304 00:12:32,136 --> 00:12:34,656 the brackets, and i; so that's going to say go 305 00:12:34,656 --> 00:12:38,196 to the ith location -- so zero, one, two, etc. -- 306 00:12:38,446 --> 00:12:40,886 get the value there, and I'm going to print it out. 307 00:12:41,526 --> 00:12:43,616 So now you'll notice that we're no longer using percent d 308 00:12:43,616 --> 00:12:47,306 for strings, but this new one, percent s. So s for string, 309 00:12:47,306 --> 00:12:49,096 which makes a lot more sense than d for integer. 310 00:12:50,136 --> 00:12:54,566 So if we run this program, you could say, make args -- 311 00:12:55,276 --> 00:12:58,366 so if I said just args, it's going to print out one thing. 312 00:12:58,696 --> 00:13:00,406 It's going to say the zeroth thing 313 00:13:00,406 --> 00:13:04,326 in the array is just the program itself including that dot slash. 314 00:13:04,526 --> 00:13:06,546 So now if I say args one, two, three, 315 00:13:07,296 --> 00:13:12,346 now see that that our argv array has four things in it. 316 00:13:12,866 --> 00:13:14,836 So in the first spot is our program name, 317 00:13:15,006 --> 00:13:18,406 and then after that in every next spot we have these words 318 00:13:18,456 --> 00:13:20,846 that I typed in that are separated by spaces. 319 00:13:21,306 --> 00:13:23,916 So remember that the counting starts at zero not one, 320 00:13:24,496 --> 00:13:25,656 so the first thing is going 321 00:13:25,656 --> 00:13:28,906 to be the first argument that's not the name of the program. 322 00:13:29,716 --> 00:13:32,666 So questions on how I'm using these command line arguments 323 00:13:32,666 --> 00:13:34,826 or argc or argv or whatever that is? 324 00:13:34,866 --> 00:13:39,406 All right moving right along. 325 00:13:39,556 --> 00:13:43,246 >> Can you say the code again, please? 326 00:13:44,586 --> 00:13:45,066 >> Oh, sure. 327 00:13:45,336 --> 00:13:47,696 So we can walk through the code again. 328 00:13:48,896 --> 00:13:49,816 So here we go. 329 00:13:49,816 --> 00:13:51,696 So no longer is it int main void. 330 00:13:51,696 --> 00:13:52,646 I now have two things. 331 00:13:53,026 --> 00:13:55,106 The first argument is always argc; 332 00:13:55,356 --> 00:13:57,816 the second argument is always an array of strings, 333 00:13:57,816 --> 00:14:01,016 so string argv, and then these brackets that say it's an array; 334 00:14:01,726 --> 00:14:03,806 and now I'm going to iterate over this array. 335 00:14:04,176 --> 00:14:07,166 I know how many things are in it so I can say, well, 336 00:14:07,166 --> 00:14:10,226 i is less than argc and I know the first thing is zero, 337 00:14:10,226 --> 00:14:11,746 so I can start my counter at zero; 338 00:14:11,746 --> 00:14:13,846 and now I'm just going to printf two things. 339 00:14:14,486 --> 00:14:16,956 So the percent d is going to get substituted with i, 340 00:14:17,256 --> 00:14:19,796 which is the position in the array that I'm at, 341 00:14:19,796 --> 00:14:22,426 and the second thing is going to be the value in that array, 342 00:14:22,566 --> 00:14:26,936 so what the actual string is that I type said. 343 00:14:27,156 --> 00:14:27,406 Yup? 344 00:14:28,486 --> 00:14:30,236 >> What's the percent s? 345 00:14:30,236 --> 00:14:33,496 >> So the percent s is just like percent d except for strings. 346 00:14:34,036 --> 00:14:36,426 So percent d for integers, percent f for athletes, 347 00:14:36,676 --> 00:14:38,326 and percent s for strings. 348 00:14:43,136 --> 00:14:44,826 So everyone kind of see what we're doing here 349 00:14:44,826 --> 00:14:45,596 and how this works? 350 00:14:46,106 --> 00:14:46,206 Yup? 351 00:14:48,016 --> 00:14:52,746 >> Argv is like the name of the array? 352 00:14:52,786 --> 00:14:56,466 >> Yes. So argv is the variable name; so it's argument values. 353 00:14:56,686 --> 00:14:58,106 And so this is just a convention. 354 00:14:58,976 --> 00:14:59,486 >> Could we [inaudible]. 355 00:15:00,686 --> 00:15:03,276 >> Yeah, so by convention we should use argc and argv. 356 00:15:03,276 --> 00:15:03,516 Yeah. 357 00:15:03,846 --> 00:15:08,586 >> In our program it's going to be like another name 358 00:15:08,586 --> 00:15:12,176 if the array's used to represent, for example, numbers? 359 00:15:13,346 --> 00:15:16,406 >> So you could, but just -- it's very common in c programs 360 00:15:16,406 --> 00:15:18,336 for there to be argc and argv, so it's just kinds 361 00:15:18,336 --> 00:15:20,466 of a convention that when people see argv, they're like, oh, 362 00:15:20,466 --> 00:15:21,806 okay, so that refers to the command not arguments. 363 00:15:21,836 --> 00:15:22,976 So it is best to keep these named as they are. 364 00:15:23,516 --> 00:15:28,616 [ Pause ] 365 00:15:29,116 --> 00:15:29,466 All right. 366 00:15:30,396 --> 00:15:30,976 So -- 367 00:15:31,516 --> 00:15:37,876 [ Pause ] 368 00:15:38,376 --> 00:15:39,436 all right. 369 00:15:39,476 --> 00:15:40,036 So that's done. 370 00:15:40,156 --> 00:15:42,816 So now we have k, we've converted this string 371 00:15:42,816 --> 00:15:44,976 to an integer, and we're ready to use it 372 00:15:44,976 --> 00:15:46,406 to encrypt our message. 373 00:15:46,916 --> 00:15:49,256 So the next thing we need to do is prompt the user 374 00:15:49,256 --> 00:15:51,186 for the string to encode. 375 00:15:52,716 --> 00:15:56,066 So remember in pset1 we were just dealing with numbers, 376 00:15:56,126 --> 00:15:58,596 like numbers of pennies and number 377 00:15:58,596 --> 00:15:59,886 of males spotting females. 378 00:15:59,936 --> 00:16:02,366 So we could just use get int or get long long. 379 00:16:02,696 --> 00:16:04,516 Now we're going to be using strings 380 00:16:04,656 --> 00:16:06,486 and so we have another function written 381 00:16:06,556 --> 00:16:09,286 in the CS50 library called "GetString." 382 00:16:09,756 --> 00:16:11,076 So this is just going through the same thing 383 00:16:11,076 --> 00:16:14,186 as get int except it's going to allow the user to type in words. 384 00:16:14,336 --> 00:16:16,896 And it will return to you, just like get int, 385 00:16:17,196 --> 00:16:18,726 a string rather than an integer. 386 00:16:19,136 --> 00:16:22,536 So I can say something like string name equals GetString, 387 00:16:22,626 --> 00:16:24,516 so I'm ready to receive input from the user. 388 00:16:24,846 --> 00:16:27,936 Once they hit enter, the value is stored in the variable name 389 00:16:27,936 --> 00:16:31,166 and I can say "your name is" and output the string, 390 00:16:31,316 --> 00:16:33,746 again with that percent s, s for string. 391 00:16:34,316 --> 00:16:38,576 Okay, so that's done. 392 00:16:38,576 --> 00:16:40,376 We now have the string from the user, 393 00:16:40,846 --> 00:16:43,066 and so now we actually need to encrypt it. 394 00:16:43,966 --> 00:16:46,556 So we need to loop over every character of the string, 395 00:16:46,556 --> 00:16:49,816 and then we're going to encrypt and output each letter. 396 00:16:50,526 --> 00:16:52,756 So how can we loop over a string? 397 00:16:53,406 --> 00:16:57,716 Well, a string is actually just an array of characters, right? 398 00:16:57,716 --> 00:17:00,076 That makes sense because a string is basically letters, 399 00:17:00,586 --> 00:17:02,636 and if each letter is a different element, 400 00:17:02,636 --> 00:17:03,806 we essentially have an array. 401 00:17:04,936 --> 00:17:09,256 So there's a special function called strlen that's going 402 00:17:09,256 --> 00:17:10,856 to return the length of a string, 403 00:17:11,096 --> 00:17:14,226 so how many characters are in the string; things like periods 404 00:17:14,226 --> 00:17:16,016 and spaces or numbers all count. 405 00:17:17,326 --> 00:17:19,706 So if I wanted to get a string from the user 406 00:17:19,706 --> 00:17:22,476 and just spit it back to them, I could use something like this. 407 00:17:22,476 --> 00:17:25,016 So I'm going to say, string word equals GetString. 408 00:17:25,236 --> 00:17:26,986 So, again, I'm going to prompt the user for a string 409 00:17:27,286 --> 00:17:28,546 and store it inside of word. 410 00:17:29,136 --> 00:17:31,266 So now I want to loop over that string. 411 00:17:31,576 --> 00:17:32,906 So in order to loop over it, 412 00:17:32,976 --> 00:17:35,286 I need to know how many characters it contains. 413 00:17:35,286 --> 00:17:38,016 So to get that, I can just use strlen. 414 00:17:38,466 --> 00:17:41,186 So I call strlen on a string and it's going to give back 415 00:17:41,186 --> 00:17:44,476 to me an integer that tells me how many characters are in it. 416 00:17:45,366 --> 00:17:47,916 Okay, so now I know where it starts and where it ends, 417 00:17:48,186 --> 00:17:50,066 and I can have a loop that goes over it. 418 00:17:50,436 --> 00:17:52,836 So start our counter off at the beginning of the string, 419 00:17:53,136 --> 00:17:55,186 we're going to go until we hit the end of the string, 420 00:17:56,306 --> 00:17:58,766 and for each letter inside of the string, 421 00:17:58,766 --> 00:17:59,816 I'm going to output it. 422 00:18:00,356 --> 00:18:03,136 So because a string is an array, which, again, we'll see more 423 00:18:03,136 --> 00:18:07,676 in lecture in section, we can access the ith element 424 00:18:07,676 --> 00:18:11,016 of the array, so zero, one, two, with the same bracket notation. 425 00:18:11,276 --> 00:18:14,016 So if I say, word, bracket, zero, that's going 426 00:18:14,016 --> 00:18:15,276 to give me the first character. 427 00:18:15,276 --> 00:18:16,476 If I say word, bracket, one, 428 00:18:16,476 --> 00:18:19,356 it's going to give me the second character, and so on. 429 00:18:20,876 --> 00:18:23,456 So questions on how we're going through each character? 430 00:18:23,526 --> 00:18:23,736 Yup? 431 00:18:23,736 --> 00:18:28,716 >> Why does it say inth length if it's not an integer? 432 00:18:28,716 --> 00:18:30,056 >> So why does it say inth length? 433 00:18:30,056 --> 00:18:31,536 Because it actually is an integer now. 434 00:18:31,996 --> 00:18:35,496 So strlen stands for "string length," which is the number 435 00:18:35,496 --> 00:18:37,036 of characters in my string. 436 00:18:37,326 --> 00:18:38,466 So that's always going to be an integer 437 00:18:38,466 --> 00:18:40,456 because we can't have negative, we don't have decimals, 438 00:18:40,676 --> 00:18:42,576 so it just makes sense for this to be an integer. 439 00:18:43,046 --> 00:18:43,146 Yup? 440 00:18:45,176 --> 00:18:47,126 >> What's actually encrypted [inaudible]? 441 00:18:49,396 --> 00:18:51,426 >> Right. So this is not encrypted. 442 00:18:51,596 --> 00:18:54,156 So this is actually just spitting the string right back 443 00:18:54,156 --> 00:18:56,976 out at the user, because we're not actually doing anything 444 00:18:57,206 --> 00:19:01,916 to word i. We're just outputting it back. 445 00:19:02,116 --> 00:19:02,316 Yup? 446 00:19:02,316 --> 00:19:04,796 >> For the string, when it's the array of characters, 447 00:19:04,796 --> 00:19:06,956 does it only contain the characters in that string 448 00:19:06,956 --> 00:19:09,606 or does it include anything else at the end? 449 00:19:09,606 --> 00:19:11,846 >> So, yeah, so it is at the end of it going 450 00:19:11,846 --> 00:19:14,696 to have this special character representing Null, 451 00:19:14,936 --> 00:19:17,556 which we'll see in lecture shortly, but, yes, 452 00:19:17,556 --> 00:19:19,956 so there's a special character at the end of every string 453 00:19:20,056 --> 00:19:21,376 so that you know when it ends, 454 00:19:21,536 --> 00:19:24,476 and that's basically how strlen determines how long the 455 00:19:24,476 --> 00:19:24,936 string is. 456 00:19:24,936 --> 00:19:27,226 It basically goes through the entire string, says, okay, 457 00:19:27,226 --> 00:19:29,616 once I find this character, that means the string is done 458 00:19:29,616 --> 00:19:30,916 and I know how many characters it has. 459 00:19:32,326 --> 00:19:32,496 Yup? 460 00:19:32,496 --> 00:19:40,846 >> Does this go through all the strings for you or do you need 461 00:19:40,936 --> 00:19:41,786 to do this separately for each [inaudible]? 462 00:19:42,026 --> 00:19:43,176 >> So this is going to go through a single string, right. 463 00:19:43,176 --> 00:19:44,916 We have one string here and it's called "word." 464 00:19:45,456 --> 00:19:48,256 So we're going to go through that one string one time 465 00:19:48,726 --> 00:19:50,946 and output every character one time. 466 00:19:51,006 --> 00:19:55,186 So if we had multiple strings and we could go through each 467 00:19:55,186 --> 00:19:58,286 of them separately or store them in a multidimensional array, 468 00:19:58,286 --> 00:20:00,576 which I'll see in section, and that's going 469 00:20:00,576 --> 00:20:05,166 to look pretty much the same. 470 00:20:05,376 --> 00:20:06,776 Other questions on how we're going 471 00:20:06,776 --> 00:20:08,086 through every letter of a string? 472 00:20:08,626 --> 00:20:08,726 Yup? 473 00:20:10,556 --> 00:20:16,316 >> Can you tell what letters are in the string or [inaudible]. 474 00:20:16,316 --> 00:20:18,156 >> So can it tell you what letters are inside 475 00:20:18,156 --> 00:20:18,936 of the string? 476 00:20:19,306 --> 00:20:21,286 Sure. So as we're going through this string, 477 00:20:21,556 --> 00:20:24,386 this word i is the current letter in the string, 478 00:20:24,806 --> 00:20:26,156 so I'm starting off at zero. 479 00:20:26,406 --> 00:20:29,776 Now, once it's at zero, this word i is going 480 00:20:29,776 --> 00:20:31,636 to be the first character in the string, 481 00:20:32,016 --> 00:20:33,126 so the actual first letter. 482 00:20:33,576 --> 00:20:35,296 So if I wanted to do something like determine 483 00:20:35,296 --> 00:20:37,566 if the sting contains a letter, than I can go 484 00:20:37,566 --> 00:20:40,656 through the entire string and say, okay, if this -- is this -- 485 00:20:40,656 --> 00:20:42,896 is my current letter equal to letter I'm looking for? 486 00:20:43,026 --> 00:20:44,936 If it's yes, then I can return I found it, if it's no, 487 00:20:44,936 --> 00:20:45,826 then I can keep looking. 488 00:20:46,306 --> 00:20:48,276 Does that make sense? 489 00:20:48,866 --> 00:20:48,956 Yup. 490 00:20:49,616 --> 00:20:52,636 >> Can a string include multiple words and, like, spaces? 491 00:20:53,226 --> 00:20:55,776 >> Yes. So can a string include multiple words and spaces? 492 00:20:56,096 --> 00:20:58,226 Yes. So you can have spaces inside of your string, 493 00:20:58,626 --> 00:21:00,726 that's fine; you can have numbers and characters. 494 00:21:01,256 --> 00:21:02,866 What you can't do is -- 495 00:21:02,866 --> 00:21:06,006 the difference being the command line argv thing, 496 00:21:06,496 --> 00:21:08,116 strings are separated by spaces, 497 00:21:08,416 --> 00:21:09,846 so you need to enclose it in quotes. 498 00:21:10,606 --> 00:21:13,426 But just with any regular string, like this one here, 499 00:21:13,476 --> 00:21:15,506 we can type spaces and it will be inside the string; 500 00:21:16,926 --> 00:21:17,996 where before argv kind 501 00:21:18,116 --> 00:21:20,266 of separates the different strings by spaces. 502 00:21:20,986 --> 00:21:24,426 Other questions? 503 00:21:25,746 --> 00:21:25,846 Yup. 504 00:21:26,856 --> 00:21:26,946 >> So [inaudible] 505 00:21:27,186 --> 00:21:29,676 >> Sure. So right now word is a string, 506 00:21:30,436 --> 00:21:33,386 and a string is just an array of characters; 507 00:21:33,386 --> 00:21:34,646 an array being a list. 508 00:21:35,566 --> 00:21:38,956 So now I can access different elements of that array, 509 00:21:39,276 --> 00:21:41,556 so different kinds of buckets that inside of the array, 510 00:21:41,626 --> 00:21:42,916 with this bracket notation. 511 00:21:43,246 --> 00:21:44,586 The name of the array, a bracket, 512 00:21:44,586 --> 00:21:46,686 and then the position I want to access. 513 00:21:47,136 --> 00:21:49,666 So for -- as i goes over the string, 514 00:21:49,666 --> 00:21:52,886 I'm going to access each bucket in my array which corresponds 515 00:21:52,886 --> 00:21:54,306 to each letter in my string. 516 00:21:54,806 --> 00:21:54,906 Yup? 517 00:21:57,836 --> 00:22:00,956 >> So a string length also gives the length 518 00:22:01,176 --> 00:22:03,246 of a string including the spaces, right? 519 00:22:03,246 --> 00:22:05,076 >> Yes. So when I call strlen, that's going 520 00:22:05,076 --> 00:22:06,866 to include literally everything in the string -- 521 00:22:06,866 --> 00:22:14,056 spaces, characters, numbers, all that stuff. 522 00:22:14,246 --> 00:22:14,656 All right. 523 00:22:14,656 --> 00:22:18,566 So now we know how to loop over every character of the string, 524 00:22:18,706 --> 00:22:21,156 and now comes the actual fun part, the encryption. 525 00:22:21,486 --> 00:22:24,896 Though what do we do with each character so that when I send it 526 00:22:24,896 --> 00:22:27,516 to someone else, they have no idea what I'm trying to say. 527 00:22:28,186 --> 00:22:31,086 So we're going to implement two different ciphers: 528 00:22:31,266 --> 00:22:32,886 The first is called the Caesar Cipher, 529 00:22:32,886 --> 00:22:36,106 and this is the formal mathy definition, but I'm not good 530 00:22:36,106 --> 00:22:38,136 at math so let's break it down. 531 00:22:38,216 --> 00:22:41,476 So c sub i is going to be the ith character 532 00:22:41,476 --> 00:22:42,466 in the cipher text. 533 00:22:42,966 --> 00:22:44,186 So we have a few components here. 534 00:22:44,276 --> 00:22:45,746 The first is the plain text. 535 00:22:46,046 --> 00:22:49,516 This is what the user typed in, the readable message. 536 00:22:50,176 --> 00:22:54,626 Now, c is going to be the cipher text or the encrypted text, 537 00:22:54,686 --> 00:22:58,736 the after I ran my algorithm, what each letter is encrypted. 538 00:22:59,396 --> 00:23:02,856 And finally we have some key which says, how am I going 539 00:23:02,856 --> 00:23:04,366 to encrypt each letter. 540 00:23:05,306 --> 00:23:08,896 So we have the ith letter in the cipher text is equal 541 00:23:08,896 --> 00:23:12,606 to the ith letter in the plain text; so the first letter 542 00:23:12,606 --> 00:23:14,616 in the encrypted message and the first letter 543 00:23:14,616 --> 00:23:19,486 and the message the user typed in is equal to that plus -- 544 00:23:19,546 --> 00:23:22,056 so the letter these are typed in -- plus some number. 545 00:23:23,366 --> 00:23:26,526 So if I said, 2, then k would be 2 in this case, 546 00:23:26,526 --> 00:23:28,316 and I can just say I want two letters 547 00:23:28,446 --> 00:23:30,076 after whatever p of i is. 548 00:23:30,626 --> 00:23:34,306 Now, this end is the module operator which we may have used 549 00:23:34,306 --> 00:23:36,826 in last pset, and this just basically says things 550 00:23:36,826 --> 00:23:37,216 should wrap. 551 00:23:37,746 --> 00:23:40,886 So after z, I shouldn't start outputting numbers and symbols, 552 00:23:40,886 --> 00:23:44,386 I should wrap back to a. So questions 553 00:23:44,386 --> 00:23:49,366 on the Caesar Cipher or how it works? 554 00:23:49,526 --> 00:23:51,096 Okay. So let's take a look at an example. 555 00:23:51,466 --> 00:23:53,856 So my string is, of course, "This is CS50." 556 00:23:53,856 --> 00:23:57,396 In this case my k is 13. 557 00:23:57,396 --> 00:24:00,076 So I'm going to start at the beginning of my string 558 00:24:00,076 --> 00:24:02,926 and I'm going through every letter and add 13 to it. 559 00:24:03,476 --> 00:24:07,816 So I start at T and 13 letters later, happens to be G. 560 00:24:08,156 --> 00:24:09,246 So you notice that I didn't -- 561 00:24:09,466 --> 00:24:12,986 because 13 would have taken me past the end of the alphabet, 562 00:24:13,116 --> 00:24:14,406 I wrapped around to the beginning. 563 00:24:15,396 --> 00:24:16,726 So I go through and do this 564 00:24:17,176 --> 00:24:20,146 for every single letter inside of my string. 565 00:24:20,146 --> 00:24:23,476 So you notice I'm not changing the spaces, 566 00:24:23,556 --> 00:24:25,416 so the spaces don't become any other symbols ; 567 00:24:25,576 --> 00:24:28,266 'm not changing the numbers, which stay 50; 568 00:24:28,266 --> 00:24:30,726 and I'm not changing the symbols like this period. 569 00:24:31,136 --> 00:24:33,536 I'm only changing the letters and I'm making sure 570 00:24:33,536 --> 00:24:35,656 that the capitalization is preserved. 571 00:24:36,086 --> 00:24:39,356 So T becomes a -- capital T becomes a capital G, 572 00:24:39,446 --> 00:24:44,716 capital C becomes a capital P, etc. So questions on how we went 573 00:24:44,766 --> 00:24:48,186 from "This is CS50" to whatever that says? 574 00:24:48,626 --> 00:24:48,726 Yup? 575 00:24:49,456 --> 00:24:53,876 >> Is it adding 13 to the ASCII number or is it adding 13 576 00:24:53,876 --> 00:24:57,976 to the alphabet number? 577 00:24:58,046 --> 00:25:00,286 >> So is it adding 13 to the ASCII value? 578 00:25:00,286 --> 00:25:02,196 So we're going to ASCII right now. 579 00:25:02,986 --> 00:25:03,846 The answer being yes. 580 00:25:04,386 --> 00:25:04,486 Yup? 581 00:25:05,936 --> 00:25:08,756 >> So we need to encrypt the numbers five-zero or no? 582 00:25:09,106 --> 00:25:10,656 >> So do we need to encrypt the numbers? 583 00:25:10,656 --> 00:25:12,596 No. We're only encrypting letters. 584 00:25:12,866 --> 00:25:15,436 >> And spaces I guess would be also [inaudible]. 585 00:25:15,536 --> 00:25:18,436 >> Yes. So numbers we do nothing and symbols we do nothing. 586 00:25:18,436 --> 00:25:19,886 Like the period stayed a period. 587 00:25:20,086 --> 00:25:21,706 And space really is just a symbol, 588 00:25:21,706 --> 00:25:23,256 there's nothing really special about a space, 589 00:25:23,496 --> 00:25:28,896 so we can just leave it alone. 590 00:25:29,046 --> 00:25:29,466 All right. 591 00:25:30,716 --> 00:25:32,456 So yes. So to answer that question, 592 00:25:32,816 --> 00:25:35,596 how are we actually adding things to letters? 593 00:25:36,546 --> 00:25:39,016 So there's something in C that's called ASCII, 594 00:25:39,316 --> 00:25:43,266 and ASCII is basically a mapping from characters to numbers. 595 00:25:43,806 --> 00:25:46,866 So in ASCII we have the character upper case A, 596 00:25:46,866 --> 00:25:49,856 and the mapping, it's going to say, this A, 597 00:25:49,856 --> 00:25:51,916 that's equal to the number 65. 598 00:25:53,056 --> 00:25:55,736 And a lower case a is equal to the number 97. 599 00:25:56,656 --> 00:25:59,426 So if you go to this ASCII table dot com, you can see a long list 600 00:25:59,426 --> 00:26:03,846 of all 127 of them that maps every number from 0 to 127 601 00:26:04,106 --> 00:26:05,726 to a different character on your keyboard, 602 00:26:06,026 --> 00:26:08,716 or maybe not on your keyboard. 603 00:26:08,846 --> 00:26:11,156 So let's take a look at what that actually means. 604 00:26:12,216 --> 00:26:16,476 So we open up our appliance, and we're going to go over here 605 00:26:16,476 --> 00:26:22,066 to ascii.c. so here we have two things: First, we're declaring a 606 00:26:22,066 --> 00:26:23,386 "char" or a character. 607 00:26:23,386 --> 00:26:25,676 So you notice that I'm single quotes here. 608 00:26:25,996 --> 00:26:27,856 Where strings I use double quotes 609 00:26:28,236 --> 00:26:30,656 and strings contain multiple characters and numbers; 610 00:26:31,046 --> 00:26:33,196 where a char is just a single character. 611 00:26:33,196 --> 00:26:34,726 We can't have more than one thing in here 612 00:26:34,726 --> 00:26:36,556 and it's going to use single quotes. 613 00:26:37,516 --> 00:26:40,046 So it also declared a corresponding number; 614 00:26:40,046 --> 00:26:41,726 so int number equals 65. 615 00:26:42,336 --> 00:26:45,956 You'll remember that the ASCII value of a or the number 616 00:26:45,956 --> 00:26:49,186 of that represents the upper case A is 65, 617 00:26:49,966 --> 00:26:53,206 so I can actually print both of these as chars. 618 00:26:53,786 --> 00:26:57,306 So this percent c like percent D for int, percent C says, 619 00:26:57,616 --> 00:27:00,136 print out the character represented by the letter. 620 00:27:00,956 --> 00:27:05,746 So I can print out upper case A, or I can convert that 65 621 00:27:06,076 --> 00:27:07,446 into the ASCII letter 622 00:27:07,736 --> 00:27:12,326 and so this will also print A. Similarly, 623 00:27:12,516 --> 00:27:14,176 I can print both as integers. 624 00:27:14,756 --> 00:27:16,906 So I can say, okay, I want the integer 625 00:27:17,106 --> 00:27:20,206 that this ASCII letter maps to, which is 65, 626 00:27:20,206 --> 00:27:22,326 and then the second one is already an integer 627 00:27:22,326 --> 00:27:24,406 so I can just print 65. 628 00:27:24,506 --> 00:27:28,416 So does that make sense what these two blocks are doing here? 629 00:27:29,386 --> 00:27:31,066 So basically back and forth 630 00:27:31,136 --> 00:27:33,886 from the ASCII number to the ASCII letter. 631 00:27:34,396 --> 00:27:34,486 Yup? 632 00:27:35,956 --> 00:27:37,836 >> Is it like implicit typecasting? 633 00:27:38,336 --> 00:27:40,466 >> Yes. So it is implicit typecasting. 634 00:27:40,466 --> 00:27:44,056 If C says, okay, I want to convert this number to an -- 635 00:27:44,056 --> 00:27:47,346 this character to an integer, I can just do that because they're 636 00:27:47,346 --> 00:27:51,836 so -- they're so closely related with ASCII. 637 00:27:52,076 --> 00:27:54,506 So now what we can do is we can do math 638 00:27:54,946 --> 00:27:57,076 with both letters and numbers. 639 00:27:57,176 --> 00:28:00,096 So here we're adding one to the char A, 640 00:28:00,096 --> 00:28:04,526 so can anyone guess what this is going to be? 641 00:28:04,746 --> 00:28:04,836 >> B. 642 00:28:04,836 --> 00:28:05,196 >> Exactly. 643 00:28:05,196 --> 00:28:06,566 I kind of gave it away unintentionally. 644 00:28:07,076 --> 00:28:08,856 So this is going to be an upper case B. 645 00:28:09,546 --> 00:28:12,096 And we can also do something like, I want to print 646 00:28:12,096 --> 00:28:15,546 out the character that's equal to the number -- 647 00:28:15,906 --> 00:28:20,186 the ASCII number representing lower case z minus the ASCII 648 00:28:20,186 --> 00:28:23,256 number representing this asterisk, and I want to print 649 00:28:23,256 --> 00:28:26,616 out the character that's represented by that number. 650 00:28:27,106 --> 00:28:34,426 So if I make and run this, I can say, make ASCII and run ASCII. 651 00:28:34,586 --> 00:28:36,246 So here we go. 652 00:28:36,246 --> 00:28:38,956 So remember, this first print is both the integer 653 00:28:38,956 --> 00:28:40,696 and the character and they're both A. Again, 654 00:28:40,796 --> 00:28:45,006 I'm taking the integer and the character and I want the number 655 00:28:45,006 --> 00:28:47,406 that it represents in ASCII, which is both 65. 656 00:28:48,376 --> 00:28:51,636 And so here I'm printing the letter A plus 1; 657 00:28:52,126 --> 00:28:54,376 notice I didn't have to say 65 plus 1. 658 00:28:54,376 --> 00:28:57,536 I can literally say the letter A plus 1, 659 00:28:57,876 --> 00:29:01,546 and C knows that you mean, oh, the number represented in ASCII 660 00:29:01,776 --> 00:29:05,356 for the upper case A plus 1 and convert that back to a letter. 661 00:29:06,026 --> 00:29:09,406 And finally, I have the subtraction of two characters 662 00:29:09,406 --> 00:29:12,086 and that's still going to be a character, just happens 663 00:29:12,086 --> 00:29:16,116 to be upper case P. So questions on ASCII 664 00:29:16,116 --> 00:29:17,586 or how that's going back and forth? 665 00:29:18,076 --> 00:29:18,206 Yup? 666 00:29:19,436 --> 00:29:21,316 >> Can you just go back to the code for one second? 667 00:29:21,536 --> 00:29:26,586 >> Sure. So back to the code, we have a character 668 00:29:27,156 --> 00:29:30,676 which has an ASCII value of 65, and a number 65 669 00:29:30,766 --> 00:29:32,816 that has an ASCII letter of upper case A, 670 00:29:33,426 --> 00:29:37,086 and I can print them out either as letters or as numbers 671 00:29:37,086 --> 00:29:39,926 because they're essentially the same thing using ASCII. 672 00:29:40,386 --> 00:29:40,486 Yup? 673 00:29:41,556 --> 00:29:46,166 >> So using the single quotes designates ASCII as opposed 674 00:29:46,546 --> 00:29:50,586 to a variable called z that's somewhere else in your code? 675 00:29:50,846 --> 00:29:51,436 >> Exactly. 676 00:29:51,436 --> 00:29:55,126 So using these single quotes says this is the value 677 00:29:55,126 --> 00:29:57,936 of my character; where using double quotes would say, 678 00:29:57,936 --> 00:30:00,446 this is the value of my string; and no quotes 679 00:30:00,446 --> 00:30:03,156 at all says this is some variable or function. 680 00:30:03,716 --> 00:30:03,826 Yup? 681 00:30:05,796 --> 00:30:09,786 >> Can you repeat again what the asterisk -- 682 00:30:09,786 --> 00:30:10,016 >> Oh, sure. 683 00:30:10,016 --> 00:30:10,486 So right here? 684 00:30:10,486 --> 00:30:10,766 >> Yeah. 685 00:30:11,246 --> 00:30:13,356 >> So both of these are just characters. 686 00:30:13,676 --> 00:30:16,226 So they're defined by ASCII to be some numbers. 687 00:30:16,586 --> 00:30:20,096 So Z is a character and the star even though it's a symbol also 688 00:30:20,096 --> 00:30:21,406 has an associated number. 689 00:30:21,846 --> 00:30:24,666 So I can just subtract those two numbers to get another number 690 00:30:24,666 --> 00:30:26,036 and then in turn another letter. 691 00:30:27,396 --> 00:30:27,616 Yup? 692 00:30:27,616 --> 00:30:29,776 >> If you flip the position of the Z 693 00:30:29,776 --> 00:30:31,906 and this asterisk would you get like an error? 694 00:30:32,586 --> 00:30:34,856 >> So if you flip the position of the Z and the asterisk. 695 00:30:35,226 --> 00:30:38,496 So if I tried to print out negative one- you're saying, 696 00:30:38,496 --> 00:30:39,866 like, try to print out the character represented 697 00:30:39,866 --> 00:30:40,656 by a negative number? 698 00:30:40,756 --> 00:30:41,976 Yeah, you apply it against some weird output thing. 699 00:30:42,516 --> 00:30:47,836 [ Pause ] 700 00:30:48,336 --> 00:30:49,736 okay, so that's ASCII. 701 00:30:50,516 --> 00:30:58,826 [ Pause ] 702 00:30:59,326 --> 00:31:02,966 So now we can utilize ASCII to encrypt our message. 703 00:31:03,586 --> 00:31:07,316 So first we want to forget -- not forget to use this modulus. 704 00:31:07,786 --> 00:31:09,536 So we want to make sure that we wraparound, 705 00:31:09,536 --> 00:31:12,966 so once I start adding 5 to the letter Z, I don't end off 706 00:31:12,966 --> 00:31:15,086 in some weird symbol land because there happened 707 00:31:15,086 --> 00:31:17,776 to be numbers and symbols defined in ASCII 708 00:31:17,776 --> 00:31:20,196 after the letters; however, 709 00:31:20,196 --> 00:31:23,206 we can't just follow the formula exactly. 710 00:31:23,206 --> 00:31:26,316 So we can't just say, okay, well, my p sub i is Z 711 00:31:26,516 --> 00:31:28,936 and my k is 2, then I'm going to mod 26. 712 00:31:28,936 --> 00:31:30,546 So what that's effectively going 713 00:31:30,546 --> 00:31:34,306 to do is create an upper bound of 26, right. 714 00:31:34,306 --> 00:31:36,536 If we're moding everything by 26, 715 00:31:36,536 --> 00:31:39,266 that means it can never be larger than 26. 716 00:31:39,886 --> 00:31:41,846 So this is going to end up being 20 which happens 717 00:31:41,846 --> 00:31:43,456 to some nonprintable character. 718 00:31:43,906 --> 00:31:46,406 But we really wanted this to be B, right? 719 00:31:46,406 --> 00:31:49,356 We wanted to go two spaces over so the first space is an A 720 00:31:49,356 --> 00:31:50,806 and then it's going to be a B. 721 00:31:51,556 --> 00:31:52,916 So that would be corresponding -- 722 00:31:53,206 --> 00:31:56,796 the ASCII value of that would be 67, where we just got 20. 723 00:31:57,876 --> 00:32:00,576 So we can't kind of exactly plug into the formula, 724 00:32:00,886 --> 00:32:03,716 but we need to somehow figure out a way to be able to add 2 725 00:32:03,756 --> 00:32:06,616 to something and then convert it back to the ASCII value 726 00:32:06,736 --> 00:32:08,026 of the letter we want to represent. 727 00:32:08,106 --> 00:32:09,766 So that's kind of your challenge 728 00:32:09,766 --> 00:32:11,296 for Caesar is how do we want to do that. 729 00:32:12,786 --> 00:32:14,236 So questions on why just kind 730 00:32:14,236 --> 00:32:15,976 of straight using the formula doesn't work? 731 00:32:16,516 --> 00:32:18,856 [ Pause ] 732 00:32:19,356 --> 00:32:19,466 Yup. 733 00:32:19,726 --> 00:32:24,906 >> Can you say something about the [inaudible]? 734 00:32:24,906 --> 00:32:26,816 >> Yes. So why are we using the mod to begin with? 735 00:32:27,006 --> 00:32:29,546 Just because there are 26 letters and we never want 736 00:32:29,546 --> 00:32:32,326 to go beyond that 26 letters. 737 00:32:32,886 --> 00:32:36,156 So we don't -- so there's effectively some -- 738 00:32:36,156 --> 00:32:39,436 like a lower bound and an upper bound of the letter -- 739 00:32:39,436 --> 00:32:41,076 of the, like, letters we can use. 740 00:32:41,436 --> 00:32:43,446 So if we mod it, we basically say, okay, I'm never going 741 00:32:43,446 --> 00:32:45,496 to go past that upper bound. 742 00:32:45,716 --> 00:32:48,826 We also need to go make sure we don't go past that lower bound. 743 00:32:48,826 --> 00:32:52,866 So we stay inside this range of characters that we can be using. 744 00:32:53,536 --> 00:32:55,106 So the mod just says, I'm never going to end 745 00:32:55,106 --> 00:32:57,426 up being some value that's too high to be a letter. 746 00:32:57,916 --> 00:33:02,496 Other questions on the Caesar Cipher? 747 00:33:03,596 --> 00:33:03,756 Yup. 748 00:33:03,756 --> 00:33:06,296 >> So for, like, this -- so Z is like 90, right, 749 00:33:06,296 --> 00:33:09,586 so -- so your 92 moding 26? 750 00:33:10,616 --> 00:33:11,246 >> Yeah. Yes. 751 00:33:11,766 --> 00:33:11,876 >> Okay. 752 00:33:13,076 --> 00:33:15,996 >> So if Z is 90, then we basically said, 753 00:33:15,996 --> 00:33:17,956 we're going to say 90 plus 2, which is 92 754 00:33:17,956 --> 00:33:21,516 or whatever the actual value is, and then mod 26 happens 755 00:33:21,516 --> 00:33:23,616 to be 20, which is not what we wanted. 756 00:33:23,616 --> 00:33:24,206 >> So actually [inaudible] -- 757 00:33:28,896 --> 00:33:34,396 the examples shows us why mod is a bad thing 758 00:33:34,396 --> 00:33:35,786 but there's another reason it's a good thing. 759 00:33:35,956 --> 00:33:40,716 I don't understand how it's a good thing [inaudible]. 760 00:33:41,256 --> 00:33:43,806 >> Sure. So it's a good thing if we do something else first. 761 00:33:44,586 --> 00:33:47,656 So we do want to mod somewhere but we also need to do something 762 00:33:47,656 --> 00:33:48,836 to make sure that we don't end 763 00:33:48,836 --> 00:33:51,636 up in a number that's always less than 26. 764 00:33:52,086 --> 00:33:56,656 >> Isn't 92 a mod 26 like 14 though [inaudible]? 765 00:33:57,886 --> 00:33:58,206 >> Oh, sure. 766 00:33:58,206 --> 00:34:02,276 So, I mean, if Z is -- the math maybe is wrong on this slide 767 00:34:02,276 --> 00:34:04,456 or something, but basically the point is that we're never going 768 00:34:04,456 --> 00:34:06,436 to get something that's what we want. 769 00:34:06,436 --> 00:34:10,996 We're always going to end up below 26 if we mod by 26. 770 00:34:11,436 --> 00:34:14,766 So we need to do something to get us back up into the letters. 771 00:34:15,876 --> 00:34:17,426 >> Oh, I got. 772 00:34:17,426 --> 00:34:18,376 >> Okay. Yup? 773 00:34:18,376 --> 00:34:21,916 >> Is 26 mod 26 zero or -- 774 00:34:21,916 --> 00:34:25,326 >> So 26 mod 26 is zero because 26 divided 775 00:34:25,326 --> 00:34:27,116 by 26 has a remainder of zero. 776 00:34:27,466 --> 00:34:32,676 Other questions? 777 00:34:33,216 --> 00:34:40,026 Okay. So just to keep in mind when you're going through this, 778 00:34:40,026 --> 00:34:43,496 remember that capitalization is important, so we don't want 779 00:34:43,496 --> 00:34:45,406 to convert upper case letters to lower case letters 780 00:34:45,406 --> 00:34:47,306 and vice versa; we want to make sure 781 00:34:47,306 --> 00:34:48,826 that letters never become symbols; 782 00:34:49,156 --> 00:34:51,636 and we want to make sure that symbols are never changed. 783 00:34:52,126 --> 00:34:57,396 So questions on Caesar before we move on to Vigenere? 784 00:34:57,936 --> 00:34:58,056 Yup? 785 00:34:59,336 --> 00:35:06,656 >> So when we encode our phrase, do all of the capitals 786 00:35:06,656 --> 00:35:09,036 and lower case [inaudible] at the same time? 787 00:35:10,406 --> 00:35:12,646 >> Yes. So when we encode our phrase, all of the capitals need 788 00:35:12,646 --> 00:35:15,346 to stay intact and all the lower case need to stay intact. 789 00:35:20,206 --> 00:35:21,616 Other questions on Caesar? 790 00:35:22,176 --> 00:35:27,296 Okay. So let's move on to Vigenere. 791 00:35:28,576 --> 00:35:32,106 So unlike Caesar, instead of specifying a number for a key, 792 00:35:32,106 --> 00:35:33,896 we're going to specify a word. 793 00:35:33,896 --> 00:35:36,406 So in this case we're basically going 794 00:35:36,406 --> 00:35:38,826 to get a different encryption of the same phrase 795 00:35:38,826 --> 00:35:39,906 which is "This is CS50." 796 00:35:39,906 --> 00:35:42,176 If I used a different word here, then I'm going 797 00:35:42,176 --> 00:35:43,706 to get a totally different encryption. 798 00:35:44,246 --> 00:35:46,596 So, again, here's our outline. 799 00:35:46,596 --> 00:35:49,316 It's very similar to Caesar because we want 800 00:35:49,316 --> 00:35:51,656 to read this word from the command line that we're going 801 00:35:51,656 --> 00:35:53,176 to be using to encrypt the string. 802 00:35:53,656 --> 00:35:56,556 We're going to prompt the user for what we want to encode. 803 00:35:56,556 --> 00:35:58,496 Then we want to do two things now: We want to loop 804 00:35:58,496 --> 00:36:01,456 over the string, and as we're looping over the string, 805 00:36:01,456 --> 00:36:03,266 we also want to loop over this keyword, 806 00:36:03,836 --> 00:36:06,476 because in each case we want to use the next letter 807 00:36:06,476 --> 00:36:08,896 of the keyword to encode the next letter of the plain text. 808 00:36:09,556 --> 00:36:11,736 And then we want to output each encoded letter. 809 00:36:12,346 --> 00:36:14,976 So first, let's just take a look at getting the input. 810 00:36:15,286 --> 00:36:18,246 It's very, very similar except it's actually a little easier 811 00:36:18,276 --> 00:36:18,576 this time. 812 00:36:18,576 --> 00:36:20,346 Because argv contains strings, 813 00:36:20,756 --> 00:36:22,626 we want our keyword to be a string. 814 00:36:22,956 --> 00:36:23,476 So we're all set. 815 00:36:23,606 --> 00:36:25,586 We don't need to call atoi again to convert 816 00:36:25,586 --> 00:36:26,716 that string to an integer. 817 00:36:27,226 --> 00:36:28,566 And just to get the plain text, 818 00:36:28,566 --> 00:36:29,916 we're going to do the same exact thing. 819 00:36:30,076 --> 00:36:32,186 We're just going to call it GetString, and then we're going 820 00:36:32,186 --> 00:36:33,896 to get the string that we want to encrypt. 821 00:36:35,126 --> 00:36:36,976 So those two steps are very similar to Caesar 822 00:36:36,976 --> 00:36:38,806 and actually a little bit easier because we don't have to worry 823 00:36:38,806 --> 00:36:40,286 about that atoi function. 824 00:36:40,286 --> 00:36:44,346 So now we want to loop over the string and we want 825 00:36:44,346 --> 00:36:46,186 to apply this different cipher. 826 00:36:46,786 --> 00:36:48,446 So the only difference in this formula 827 00:36:48,446 --> 00:36:52,236 than from the Caesar formula is now we have this k subj. 828 00:36:53,006 --> 00:36:55,176 So before, this k was the same thing 829 00:36:55,446 --> 00:36:57,346 for every letter inside of the plain text. 830 00:36:57,716 --> 00:36:59,516 Now this k is going to vary. 831 00:36:59,516 --> 00:37:04,966 So now it's the jth character, so not i because my plain text 832 00:37:04,966 --> 00:37:06,796 and my key can have different lengths, 833 00:37:06,846 --> 00:37:09,706 so they're not always -- i and j are not always the same thing -- 834 00:37:10,206 --> 00:37:12,416 so the jth character in my keyword. 835 00:37:12,416 --> 00:37:15,556 So I'm going to iterate over my keyword; once I reach the end 836 00:37:15,556 --> 00:37:17,276 of the keyword, I'm going to start over 837 00:37:17,486 --> 00:37:18,576 and start iterating again. 838 00:37:19,306 --> 00:37:22,226 And, again, we want to make sure we mod by 26 so that things wrap 839 00:37:22,316 --> 00:37:23,916 and I don't end up printing symbols. 840 00:37:24,466 --> 00:37:26,956 So here's an example. 841 00:37:27,166 --> 00:37:28,476 So now we have "This! 842 00:37:28,476 --> 00:37:30,286 is CS50," which sometimes happens 843 00:37:30,286 --> 00:37:32,956 when David's mic is too loud, and we're adding 844 00:37:32,956 --> 00:37:34,576 in the keyword "tommy." 845 00:37:34,576 --> 00:37:37,506 So you'll notice that in each letter of my -- 846 00:37:37,506 --> 00:37:41,656 each letter of my plain text is using the next letter of tommy. 847 00:37:41,736 --> 00:37:43,476 So I'm not just adding 13 every time. 848 00:37:43,776 --> 00:37:46,216 The first time I'm adding a t to an m 849 00:37:46,216 --> 00:37:49,036 which produces an M. You'll notice, again, capitalization 850 00:37:49,036 --> 00:37:50,306 and things like that are preserved. 851 00:37:50,526 --> 00:37:52,506 Symbols are untouched, spaces are untouched, 852 00:37:52,626 --> 00:37:53,966 numbers are untouched. 853 00:37:54,716 --> 00:37:56,786 So you'll notice as we're going through each letter 854 00:37:56,786 --> 00:37:59,026 of the plain text, we're going through each next letter 855 00:37:59,026 --> 00:38:01,566 of the keyword, and once I've reached the end of my keyword, 856 00:38:01,826 --> 00:38:03,706 which is at the i, I'm going to start over. 857 00:38:03,706 --> 00:38:07,516 So I'm going to go back to t, and then continue on with o-m. 858 00:38:08,506 --> 00:38:10,606 So questions on this Vigenere's working? 859 00:38:10,606 --> 00:38:10,796 Yup. 860 00:38:10,796 --> 00:38:11,806 >> [Inaudible] the [inaudible] number like from ASCII 861 00:38:12,116 --> 00:38:17,006 of that character, like of -- like m is 13 and we're adding 862 00:38:17,086 --> 00:38:20,196 that to the i, is that what we're doing. 863 00:38:20,276 --> 00:38:21,006 >> Yes. Essentially, yes. 864 00:38:21,056 --> 00:38:23,756 So that this t has some ASCII value, 865 00:38:23,966 --> 00:38:26,566 my lower case t has some value, and we're going 866 00:38:26,566 --> 00:38:27,636 to add them together and we're going 867 00:38:27,636 --> 00:38:29,006 to produce some other value. 868 00:38:29,136 --> 00:38:29,506 Yup? 869 00:38:30,166 --> 00:38:32,886 >> So what if your keyword was longer than your string, 870 00:38:33,496 --> 00:38:33,806 would you [inaudible]? 871 00:38:34,746 --> 00:38:36,566 >> So if your keyword is longer than your string, 872 00:38:36,566 --> 00:38:38,666 then you basically don't ever need to restart the keyword. 873 00:38:38,666 --> 00:38:41,506 You could just use it up until your plain text it done 874 00:38:41,506 --> 00:38:42,616 and we don't ever need to go back 875 00:38:42,616 --> 00:38:43,636 to the beginning of the keyword. 876 00:38:44,026 --> 00:38:48,696 >> Oh, yeah, okay. 877 00:38:48,896 --> 00:38:51,396 >> Okay. So now you'll notice 878 00:38:51,426 --> 00:38:54,096 that we're changing each character by a different amount. 879 00:38:54,666 --> 00:38:57,006 So after each letter, we want to go to the next letter 880 00:38:57,006 --> 00:39:00,206 in the plain text and the next letter in the keyword 881 00:39:00,586 --> 00:39:03,146 which could result in going back to the beginning of the keyword 882 00:39:03,316 --> 00:39:05,466 but not back to the beginning of the plain text because we want 883 00:39:05,466 --> 00:39:06,866 to make sure we get through the whole thing. 884 00:39:08,286 --> 00:39:11,216 But we don't want to go to the next letter of our keyword 885 00:39:11,406 --> 00:39:12,806 if we encounter a symbol. 886 00:39:13,186 --> 00:39:16,156 So you'll notice here that we have the exclamation mark after 887 00:39:16,156 --> 00:39:20,296 "this," so that didn't kind of burnout our y. We saved the y 888 00:39:20,296 --> 00:39:22,346 until the next thing that was a letter. 889 00:39:23,696 --> 00:39:26,126 So if you encounter something that is not going 890 00:39:26,126 --> 00:39:28,446 to be encrypted, including numbers and symbols, 891 00:39:28,556 --> 00:39:32,956 you don't want to move to the next space in your keyword. 892 00:39:32,956 --> 00:39:35,426 So that means that somehow we need to keep track 893 00:39:35,426 --> 00:39:38,716 of both our position in the plain text and our position 894 00:39:38,716 --> 00:39:41,746 in the keyword, so we know what two values to add together 895 00:39:41,746 --> 00:39:43,636 and when we need to start going back 896 00:39:43,636 --> 00:39:44,696 to the beginning of the keyword. 897 00:39:45,156 --> 00:39:48,526 So having the length of both using maybe strlen is probably 898 00:39:48,526 --> 00:39:50,526 going to be helpful as you're going through your program. 899 00:39:51,026 --> 00:39:55,846 So that's kind of our outline for Vigenere. 900 00:39:55,846 --> 00:39:57,706 It's very similar to Caesar, 901 00:39:57,706 --> 00:39:59,656 the only difference being we're not just going 902 00:39:59,656 --> 00:40:02,026 to simply add a number each time, but we're going 903 00:40:02,026 --> 00:40:04,856 to add a different number each time making sure to restart 904 00:40:04,856 --> 00:40:06,886 at the beginning of our keyword as necessary. 905 00:40:06,886 --> 00:40:09,146 So does that make sense 906 00:40:09,146 --> 00:40:10,606 to everyone how we can go about doing that? 907 00:40:11,516 --> 00:40:14,906 [ Pause ] 908 00:40:15,406 --> 00:40:17,316 Okay. So before we wrap up, 909 00:40:17,806 --> 00:40:19,926 let's talk about a little bit about design. 910 00:40:20,116 --> 00:40:21,976 Don't forget, in this pset and every pset, 911 00:40:22,246 --> 00:40:25,216 style is very important and very easy points to get, 912 00:40:25,576 --> 00:40:27,636 so just make sure that your indentation is clear, 913 00:40:27,816 --> 00:40:31,346 you're commenting your code, your variable names make sense. 914 00:40:31,346 --> 00:40:33,176 Now you're going to start creating functions 915 00:40:33,426 --> 00:40:35,696 so having function names that make sense and not things 916 00:40:35,696 --> 00:40:38,516 like single letters or anything like that. 917 00:40:39,696 --> 00:40:43,276 So now, design, another access you'll be graded on. 918 00:40:43,276 --> 00:40:44,646 So one of the major things 919 00:40:44,646 --> 00:40:48,656 that shows good code design is this DRY principle which says 920 00:40:48,726 --> 00:40:49,746 "Don't Repeat Yourself." 921 00:40:50,436 --> 00:40:52,276 So if you notice that when you're writing code you're kind 922 00:40:52,276 --> 00:40:55,246 of like, oh, I got to copy/paste something and use it over here, 923 00:40:55,246 --> 00:40:56,766 you know, copy/paste it, use it over here. 924 00:40:56,956 --> 00:40:59,136 That's usually not a good thing because you're kind 925 00:40:59,196 --> 00:41:01,966 of rewriting code that you don't need to rewrite. 926 00:41:02,756 --> 00:41:05,406 Similarly, if you're kind of rewriting the same logic 927 00:41:05,456 --> 00:41:07,136 but just tweaking it a little bit all over the place, 928 00:41:07,136 --> 00:41:09,436 that's probably also not good design. 929 00:41:09,776 --> 00:41:12,416 So the solution to both of these problems, this copy/pasting 930 00:41:12,416 --> 00:41:14,456 and kind of just tweaking logic depending on where you're 931 00:41:14,456 --> 00:41:16,736 on the program, is to use functions. 932 00:41:17,346 --> 00:41:18,896 So because a function is a block 933 00:41:18,896 --> 00:41:20,636 of code you can call whenever you want, 934 00:41:20,916 --> 00:41:22,766 it's effectively reusable, right? 935 00:41:22,766 --> 00:41:25,246 I can use the function to display a verse here; 936 00:41:25,246 --> 00:41:27,156 I can use a function to display a verse 937 00:41:27,156 --> 00:41:30,586 in Old Man somewhere else; and that will allow me to not have 938 00:41:30,586 --> 00:41:32,526 to write out the "display this verse 939 00:41:32,526 --> 00:41:33,986 in old man" a bunch of times. 940 00:41:33,986 --> 00:41:37,266 So it also allows you to organize your code a lot better 941 00:41:37,506 --> 00:41:39,386 because you can break up these large problems 942 00:41:39,576 --> 00:41:40,746 into smaller problems. 943 00:41:41,076 --> 00:41:43,336 So you might want to say, okay, I have this large problem . 944 00:41:43,336 --> 00:41:46,976 I have this word and I need to output some cipher text. 945 00:41:47,526 --> 00:41:50,576 So a smaller problem might be, okay, I have this letter 946 00:41:51,046 --> 00:41:53,756 and I need to output the encrypted form of this letter. 947 00:41:54,206 --> 00:41:57,686 So putting that together, I can keep using this letter function 948 00:41:57,686 --> 00:41:59,566 and eventually have an entire word. 949 00:42:00,356 --> 00:42:03,156 So it's always good to take some big problem and break it up into 950 00:42:03,156 --> 00:42:05,266 as small a problems as possible and then kind 951 00:42:05,266 --> 00:42:07,716 of combine these smaller problems to get the solution 952 00:42:08,026 --> 00:42:08,886 to the bigger problem. 953 00:42:09,566 --> 00:42:12,026 So having functions that do a single task even 954 00:42:12,026 --> 00:42:13,906 if it's something small is a good idea. 955 00:42:13,906 --> 00:42:19,626 So last week a lot of people were asking, well, 956 00:42:19,626 --> 00:42:21,656 where is this list of functions or something. 957 00:42:22,066 --> 00:42:26,016 So we have on cs50.net a very large list 958 00:42:26,016 --> 00:42:29,126 of all the helpful functions in C. So this just happens 959 00:42:29,126 --> 00:42:35,246 to be cs50.net/resources/cpp reference.com/stdstring; 960 00:42:35,866 --> 00:42:39,116 and so these are all the relevant string functions you 961 00:42:39,116 --> 00:42:39,936 might want to use. 962 00:42:40,276 --> 00:42:42,196 So this is just a very comprehensive list. 963 00:42:42,746 --> 00:42:45,396 I can navigate here to just C Reference, 964 00:42:45,736 --> 00:42:48,476 and I can see here's my string functions, my math functions. 965 00:42:48,476 --> 00:42:51,226 I can click this, see a bunch of helpful math functions. 966 00:42:51,516 --> 00:42:55,356 Want to click on one, it tells you exactly what header file you 967 00:42:55,356 --> 00:42:57,776 need to include, and it tells you what the function does. 968 00:42:58,456 --> 00:43:00,636 So in this case this is the absolute value function. 969 00:43:00,806 --> 00:43:03,546 So in order to use it, I need to make sure 970 00:43:03,546 --> 00:43:07,696 that I include stdlib.h, and then I have this function 971 00:43:07,836 --> 00:43:08,766 "abs" available to me. 972 00:43:09,206 --> 00:43:11,796 So it takes one argument, it's an integer, it's a number, 973 00:43:11,996 --> 00:43:15,226 and you can see it returns the absolute value of that number. 974 00:43:16,146 --> 00:43:18,056 So that link might be really helpful if you're looking 975 00:43:18,056 --> 00:43:20,556 for this comprehensive list of C functions 976 00:43:20,556 --> 00:43:22,356 because we haven't really been using C that much 977 00:43:22,356 --> 00:43:24,916 so you might be not aware that some function exists, 978 00:43:25,106 --> 00:43:27,036 so you might want to poke around that page to find 979 00:43:27,036 --> 00:43:32,486 out if what you're trying to do has already been written. 980 00:43:32,556 --> 00:43:36,046 So just make sure before you leave to make sure 981 00:43:36,046 --> 00:43:37,416 that you update your appliance 982 00:43:37,416 --> 00:43:39,196 so you have the latest version of everything. 983 00:43:39,896 --> 00:43:43,936 So any last questions on Caesar, Vigenere, 984 00:43:43,936 --> 00:43:47,236 the pset sections, logistics, anything? 985 00:43:47,686 --> 00:43:47,856 Yup? 986 00:43:48,346 --> 00:43:50,846 >> Well, what's up with returning 0 and returning 1? 987 00:43:51,386 --> 00:43:54,216 >> Sure. So inside of the pset it says, you need to return 1 988 00:43:54,216 --> 00:43:55,896 if there's some kind of error occurred, right? 989 00:43:56,236 --> 00:44:00,166 So this is another convention in C. When a program goes 990 00:44:00,166 --> 00:44:02,816 through successfully, usually at the end of main you might say, 991 00:44:02,816 --> 00:44:04,896 return 0, which means, return 992 00:44:04,896 --> 00:44:07,596 if there are zero errors in my program. 993 00:44:07,596 --> 00:44:09,906 So if you -- if our program returns something that's other 994 00:44:09,906 --> 00:44:12,956 than 0, that usually signifies that something went wrong. 995 00:44:13,286 --> 00:44:16,116 So, like, well, some of you were using Virtual Box, 996 00:44:16,116 --> 00:44:18,616 you sometimes saw like error code 355 something. 997 00:44:19,216 --> 00:44:21,886 So this is kind of like Virtual Box just returned some number 998 00:44:21,886 --> 00:44:22,826 that wasn't 0. 999 00:44:23,486 --> 00:44:25,346 So in this case we ask you to return 1. 1000 00:44:25,346 --> 00:44:29,156 So the special value 1 just says to the programmers, 1001 00:44:29,156 --> 00:44:31,496 anyone running your code, or automated tests or anything 1002 00:44:31,496 --> 00:44:33,226 like that, that something went wrong. 1003 00:44:34,406 --> 00:44:36,166 And so that's why we want you to return 0 1004 00:44:36,166 --> 00:44:38,246 if everything went well; and if something goes wrong, 1005 00:44:38,516 --> 00:44:39,536 then you want to return 1. 1006 00:44:40,046 --> 00:44:49,016 Other questions on the pset? 1007 00:44:49,196 --> 00:44:49,506 All right. 1008 00:44:49,566 --> 00:44:51,126 If not, then good luck with Crypto. 1009 00:44:51,126 --> 00:44:53,706 Don't forget about help.cs50.net. 1010 00:44:53,706 --> 00:44:55,066 You'll get really fast answers. 1011 00:44:55,416 --> 00:44:55,976 And good luck. 1012 00:44:56,516 --> 00:45:03,520 [ Silence ]