1 00:00:00,000 --> 00:00:00,720 2 00:00:00,720 --> 00:00:03,190 >> DAVID J. MALAN: Suppose that I'd like to implement a program that prompts 3 00:00:03,190 --> 00:00:06,650 the user for a string and then proceeds to capitalize their input, 4 00:00:06,650 --> 00:00:09,790 converting any lowercase letters that they type to uppercase. 5 00:00:09,790 --> 00:00:11,770 Well, let's go ahead and implement that program. 6 00:00:11,770 --> 00:00:18,760 >> Let's first include cs50.h followed by include stdio.h. 7 00:00:18,760 --> 00:00:23,990 And so that we can use strlen, let's include string.h. 8 00:00:23,990 --> 00:00:29,860 >> Let's next declare main as int main void and let's now proceed to prompt 9 00:00:29,860 --> 00:00:31,600 the user for a string. 10 00:00:31,600 --> 00:00:34,840 Printf, let's prompt them for some input. 11 00:00:34,840 --> 00:00:36,460 Now let's declare a string-- 12 00:00:36,460 --> 00:00:37,630 we'll call it s-- 13 00:00:37,630 --> 00:00:44,010 and store in it the result of calling the cs50 library function GetString. 14 00:00:44,010 --> 00:00:48,850 >> Let's now proceed to iterate over each of the characters in s, capitalizing 15 00:00:48,850 --> 00:00:50,650 any lowercase letters that we see. 16 00:00:50,650 --> 00:00:53,630 For int, i get 0. 17 00:00:53,630 --> 00:00:58,780 Let's also declare n as being equal to the string length of s so that we can 18 00:00:58,780 --> 00:01:03,590 iterate from i up until n, the length of s, and on each 19 00:01:03,590 --> 00:01:05,760 iteration increment i. 20 00:01:05,760 --> 00:01:09,970 >> And then inside of this loop, let's first check is the current letter-- 21 00:01:09,970 --> 00:01:12,270 the i-th letter of s, so to speak-- 22 00:01:12,270 --> 00:01:14,170 a lowercase letter. 23 00:01:14,170 --> 00:01:23,090 If s bracket i is greater than or equal to lowercase a, and it's less 24 00:01:23,090 --> 00:01:26,900 than or equal to lowercase z-- 25 00:01:26,900 --> 00:01:30,860 Now if we want to convert a lowercase letter to uppercase, recall first that 26 00:01:30,860 --> 00:01:35,810 in ASCII a lowercase a is 97 and an uppercase A is 65. 27 00:01:35,810 --> 00:01:41,020 Meanwhile, a lowercase b is 98, and an uppercase B is 66. 28 00:01:41,020 --> 00:01:44,180 >> If we continue to look at that pattern, we'll see that the lowercase 29 00:01:44,180 --> 00:01:49,240 letters are always 32 values higher than the uppercase letters. 30 00:01:49,240 --> 00:01:53,490 So if we want to convert from lowercase to uppercase, it should 31 00:01:53,490 --> 00:01:57,210 suffice, really, to subtract 32 from the user's input. 32 00:01:57,210 --> 00:02:01,330 Or more generally, just subtract that difference between a lowercase a and a 33 00:02:01,330 --> 00:02:02,310 capital A. 34 00:02:02,310 --> 00:02:03,410 >> How to express that? 35 00:02:03,410 --> 00:02:04,640 Well, let's do it in code. 36 00:02:04,640 --> 00:02:09,960 Printf, quote, unquote "%c" to print the current character, followed by 37 00:02:09,960 --> 00:02:18,500 printing whatever's in s bracket i minus the result of doing lowercase a 38 00:02:18,500 --> 00:02:22,660 minus uppercase A semicolon. 39 00:02:22,660 --> 00:02:27,010 In other words, this parenthetical expression, little a minus big A, is 40 00:02:27,010 --> 00:02:29,360 going to return to us at the end of the day 32. 41 00:02:29,360 --> 00:02:31,230 But I don't have to remember that it's 32. 42 00:02:31,230 --> 00:02:34,150 I can allow the computer to figure out what the difference between lowercase 43 00:02:34,150 --> 00:02:35,990 a and capital A is. 44 00:02:35,990 --> 00:02:39,610 >> Meanwhile, once I know that difference, I can subtract it from s 45 00:02:39,610 --> 00:02:43,710 bracket i, which will take what's presumably a lowercase letter to a 46 00:02:43,710 --> 00:02:48,100 lower value, namely a value that maps onto an uppercase equivalent. 47 00:02:48,100 --> 00:02:51,240 Let's now save, compile, and run this program. 48 00:02:51,240 --> 00:02:57,760 Make capitalize dot slash capitalized. 49 00:02:57,760 --> 00:03:01,290 And my input will be hello. 50 00:03:01,290 --> 00:03:02,920 And there we have, hello. 51 00:03:02,920 --> 00:03:05,570 >> Now my prompt, admittedly, is a bit ugly, because we've 52 00:03:05,570 --> 00:03:07,810 omitted one bit of printing. 53 00:03:07,810 --> 00:03:09,370 And let's go back and add that. 54 00:03:09,370 --> 00:03:12,890 At the very bottom of this program, I'm very simply, and largely for 55 00:03:12,890 --> 00:03:18,440 aesthetic purpose, going to add printf, quote, unquote backslash n. 56 00:03:18,440 --> 00:03:22,330 Let's resave this file, recompile, rerun. 57 00:03:22,330 --> 00:03:28,090 >> Make capitalize, dot slash capitalize. 58 00:03:28,090 --> 00:03:32,540 Again, for input I'll provide "hello" in all lower case and now hit Enter, 59 00:03:32,540 --> 00:03:34,550 and "hello," much more cleanly printed. 60 00:03:34,550 --> 00:03:36,432