1 00:00:00,000 --> 00:00:00,280 2 00:00:00,280 --> 00:00:02,240 >> DAVID MALAN: Let's now refine this program a bit further. 3 00:00:02,240 --> 00:00:06,510 Wouldn't it be nice if toupper could capitalize a letter if lowercase, and 4 00:00:06,510 --> 00:00:08,880 if not lowercase, pass it through unchanged? 5 00:00:08,880 --> 00:00:12,110 In other words, could I replace my if-else block with a 6 00:00:12,110 --> 00:00:13,960 single call to toupper? 7 00:00:13,960 --> 00:00:16,000 >> Well, to answer this question, I'd best consult the 8 00:00:16,000 --> 00:00:17,630 documentation for toupper. 9 00:00:17,630 --> 00:00:22,100 To do so, let me open a larger terminal window outside of gedit, and 10 00:00:22,100 --> 00:00:26,060 then type man toupper, thereby signifying that I'd like to open the 11 00:00:26,060 --> 00:00:29,060 so-called man page for the function toupper. 12 00:00:29,060 --> 00:00:31,580 Upon hitting Enter, I see a screen like this one. 13 00:00:31,580 --> 00:00:34,750 And now notice that they seem to have combined the documentation for toupper 14 00:00:34,750 --> 00:00:37,010 upper with that for tolower. 15 00:00:37,010 --> 00:00:38,010 But no matter. 16 00:00:38,010 --> 00:00:41,640 >> You'll notice under the synopsis that I'm reminded that, indeed, to use this 17 00:00:41,640 --> 00:00:45,220 function, I must include the header file ctype.h. 18 00:00:45,220 --> 00:00:49,520 Below that, you see that toupper is declared as returning an int and 19 00:00:49,520 --> 00:00:52,720 accepting an int, which is a bit curious, since thus far, I've been 20 00:00:52,720 --> 00:00:54,370 manipulating chars only. 21 00:00:54,370 --> 00:00:55,310 But that's OK. 22 00:00:55,310 --> 00:00:59,070 It turns out that this is just a convention, and we can still use c as 23 00:00:59,070 --> 00:01:01,990 though it's a char, even though it's declared as an int. 24 00:01:01,990 --> 00:01:05,850 >> Now, in the description here, I see that toupper converts the letter c to 25 00:01:05,850 --> 00:01:07,980 uppercase if possible. 26 00:01:07,980 --> 00:01:11,520 And under return value, I see that the value returned is that of the 27 00:01:11,520 --> 00:01:15,320 converted letter, or c, if the conversion was not possible. 28 00:01:15,320 --> 00:01:17,120 And there's the insight I was looking for. 29 00:01:17,120 --> 00:01:21,060 If c is not, in fact, a lowercase letter, it seems that toupper will 30 00:01:21,060 --> 00:01:23,450 simply pass it along unchanged. 31 00:01:23,450 --> 00:01:25,750 >> So let's now return to my code. 32 00:01:25,750 --> 00:01:30,480 And let's now remove the entirety of this conditional block and replace it, 33 00:01:30,480 --> 00:01:40,240 quite simply, with printf %c comma toupper of s bracket i close paren 34 00:01:40,240 --> 00:01:45,120 close paren semicolon, thereby signifying that I'd like to replace %c 35 00:01:45,120 --> 00:01:49,050 with the capitalization of the ith letter in s if the ith 36 00:01:49,050 --> 00:01:50,530 letter in s is lowercase. 37 00:01:50,530 --> 00:01:53,710 Or even if it's not, it will be passed through unchanged. 38 00:01:53,710 --> 00:01:58,790 >> Let's now compile this program with make capitalize2, and run it with 39 00:01:58,790 --> 00:02:01,800 capitalize2, again, typing hello. 40 00:02:01,800 --> 00:02:03,800 And indeed, we get back uppercase. 41 00:02:03,800 --> 00:02:05,090 Let's again type David. 42 00:02:05,090 --> 00:02:06,340 And it, too, is uppercase. 43 00:02:06,340 --> 00:02:09,380