1 00:00:00,000 --> 00:00:00,560 2 00:00:00,560 --> 00:00:02,460 >> DAVID J. MALAN: Let's take things one step further, now. 3 00:00:02,460 --> 00:00:05,450 Suppose that I don't just want to print all of my program's 4 00:00:05,450 --> 00:00:06,630 command line arguments. 5 00:00:06,630 --> 00:00:10,490 But I, instead, want to print all of the individual characters in each of 6 00:00:10,490 --> 00:00:14,060 my command line arguments, one per line, so that upon running this 7 00:00:14,060 --> 00:00:17,490 program, I simply see a stream of characters on the screen that 8 00:00:17,490 --> 00:00:20,700 collectively are from my sequence of command line arguments. 9 00:00:20,700 --> 00:00:21,980 >> Well, how can I do this? 10 00:00:21,980 --> 00:00:26,540 I can keep intact my loop from before whereby I iterative from i up until 11 00:00:26,540 --> 00:00:30,060 ARGC in order to integrate over the command line arguments themselves. 12 00:00:30,060 --> 00:00:34,460 But now, within each iteration of that loop, I need to now iterate over each 13 00:00:34,460 --> 00:00:38,550 of the characters or chars in a specific string. 14 00:00:38,550 --> 00:00:40,620 So to do that, I need one more loop. 15 00:00:40,620 --> 00:00:42,090 >> Let's add that. 16 00:00:42,090 --> 00:00:46,890 for int, let's call it j, equals 0. 17 00:00:46,890 --> 00:00:49,980 Now I'm going to need to integrate up until the length of the 18 00:00:49,980 --> 00:00:52,310 current string in ARGV. 19 00:00:52,310 --> 00:00:55,910 So let me give myself another local variable, we'll call it n, and set 20 00:00:55,910 --> 00:01:00,120 that equal to the string length of the current argument, which is going to be 21 00:01:00,120 --> 00:01:04,230 found in ARGV bracket i semicolon. 22 00:01:04,230 --> 00:01:08,500 >> Let me now iterate from j up until n followed by an 23 00:01:08,500 --> 00:01:10,400 increment on each iteration. 24 00:01:10,400 --> 00:01:13,850 Let's now print the individual characters on the screen. 25 00:01:13,850 --> 00:01:21,920 Printf %c this time followed by a new line, close quote, comma, and now I 26 00:01:21,920 --> 00:01:25,565 need the j-th character in the i-th argument. 27 00:01:25,565 --> 00:01:30,240 >> So to get at that, I can simply specify ARV bracket i to 28 00:01:30,240 --> 00:01:32,050 get the i-th argument. 29 00:01:32,050 --> 00:01:36,140 And then I can dive in one level deeper and do a secondary index of 30 00:01:36,140 --> 00:01:40,420 bracket j, close parenthesis, semicolon, Save. 31 00:01:40,420 --> 00:01:43,610 And let's now compile and run this program. 32 00:01:43,610 --> 00:01:46,630 >> Make ARGV2-- 33 00:01:46,630 --> 00:01:48,440 not quite what I was expecting. 34 00:01:48,440 --> 00:01:50,740 Let's scroll up to the first of these error messages. 35 00:01:50,740 --> 00:01:54,540 And in red here, we see error, implicitly declaring library function 36 00:01:54,540 --> 00:01:56,290 strlen with type-- 37 00:01:56,290 --> 00:01:57,380 oh, wait a minute. 38 00:01:57,380 --> 00:02:00,340 This is the first time I've used strlen, and I didn't anticipate 39 00:02:00,340 --> 00:02:04,970 wanting it at first, so I didn't include the header file that declares 40 00:02:04,970 --> 00:02:06,290 a string length function. 41 00:02:06,290 --> 00:02:09,419 >> So at the top of my file, recall, I have CS50.h. 42 00:02:09,419 --> 00:02:11,080 I have standard io.h. 43 00:02:11,080 --> 00:02:14,500 But it turns out, and I know this from reading the documentation, that I need 44 00:02:14,500 --> 00:02:18,320 a third header file if I, indeed, want to use the strlen function. 45 00:02:18,320 --> 00:02:22,760 That function is declared in string.h. 46 00:02:22,760 --> 00:02:25,230 >> Let's resave the file, compile, and run. 47 00:02:25,230 --> 00:02:32,360 48 00:02:32,360 --> 00:02:35,570 And this time, we don't just see each of the arguments. 49 00:02:35,570 --> 00:02:40,470 We instead see each of the arguments one character at a time. 50 00:02:40,470 --> 00:02:43,165