1 00:00:00,000 --> 00:00:00,340 2 00:00:00,340 --> 00:00:02,690 >> DAVID MALAN: Suppose we'd like to write a program that prompts the user 3 00:00:02,690 --> 00:00:05,100 for a string and then capitalizes that string. 4 00:00:05,100 --> 00:00:09,000 Well, let's start with some familiar code declaring a string called s and 5 00:00:09,000 --> 00:00:11,380 assigning it the return value of getstring. 6 00:00:11,380 --> 00:00:14,910 And let's now proceed to iterate over the characters in this string. 7 00:00:14,910 --> 00:00:16,000 Well, how to do that? 8 00:00:16,000 --> 00:00:19,350 It turns out that a string is just a sequence of characters, but more 9 00:00:19,350 --> 00:00:23,320 properly, a string is an array of characters, which means we can use 10 00:00:23,320 --> 00:00:27,590 square bracket notation to index into a string and get at individual 11 00:00:27,590 --> 00:00:28,680 characters. 12 00:00:28,680 --> 00:00:30,980 >> In other words, we can do the following. 13 00:00:30,980 --> 00:00:36,730 For int, i gets 0, and n gets, say, the length of s, using 14 00:00:36,730 --> 00:00:37,530 our function [? stir ?] 15 00:00:37,530 --> 00:00:38,630 [? line, ?] 16 00:00:38,630 --> 00:00:40,450 i is less than n i++. 17 00:00:40,450 --> 00:00:45,290 In other words, with this loop, we will iterate over all n letters 18 00:00:45,290 --> 00:00:46,670 in the string s. 19 00:00:46,670 --> 00:00:51,680 And within this loop, I'm going to check, if the i-th character in s 20 00:00:51,680 --> 00:00:57,630 greater than or equal to lowercase a, and the i-th character is less than or 21 00:00:57,630 --> 00:01:02,370 equal to a lowercase c, then I want to proceed to capitalize that letter. 22 00:01:02,370 --> 00:01:08,030 In other words, I want to print out %c as a placeholder and substitute in for 23 00:01:08,030 --> 00:01:11,120 that placeholder s bracket i. 24 00:01:11,120 --> 00:01:14,390 >> But then I need to convert s bracket i to uppercase. 25 00:01:14,390 --> 00:01:18,930 To do this, I can simply subtract whatever the difference is between 26 00:01:18,930 --> 00:01:24,120 lowercase a and capital A. Well, I actually do recall that capital A is 27 00:01:24,120 --> 00:01:27,160 65 in ASCII, and lowercase a is 97. 28 00:01:27,160 --> 00:01:29,210 So the difference is technically 32. 29 00:01:29,210 --> 00:01:31,620 So I could just hard code 32 here. 30 00:01:31,620 --> 00:01:33,870 But I might not necessarily remember those numbers. 31 00:01:33,870 --> 00:01:37,250 >> And moreover, what if they vary by computer? 32 00:01:37,250 --> 00:01:38,520 Most likely they're not. 33 00:01:38,520 --> 00:01:42,460 But the point remains that I can still generalize that arithmetic expression 34 00:01:42,460 --> 00:01:46,550 as just whatever the difference is between a lowercase a and a capital A 35 00:01:46,550 --> 00:01:50,880 is what I want to subtract off from this particular lowercase letter. 36 00:01:50,880 --> 00:01:54,500 >> Now, if this particular letter is not lowercase, I simply 37 00:01:54,500 --> 00:01:56,620 want to print it out. 38 00:01:56,620 --> 00:02:01,110 printf, %c as my placeholder, s bracket i. 39 00:02:01,110 --> 00:02:05,150 At the bottom of this program, let's simply print out newline so that my 40 00:02:05,150 --> 00:02:07,520 prompt appears on a new line of its own. 41 00:02:07,520 --> 00:02:11,540 >> Let's now compile this program with make capitalize0. 42 00:02:11,540 --> 00:02:13,900 Let's run it with capitalize0. 43 00:02:13,900 --> 00:02:17,190 And let's type in a word like hello in all lowercase. 44 00:02:17,190 --> 00:02:19,960 I get back HELLO in uppercase as expected. 45 00:02:19,960 --> 00:02:25,210 But let's try one more test, this time with my own name, D-A-V-I-D, but with 46 00:02:25,210 --> 00:02:28,170 the first D capitalized, just in case I messed something up 47 00:02:28,170 --> 00:02:29,430 with that first char. 48 00:02:29,430 --> 00:02:34,250 Enter, and D-A-V-I-D in uppercase is printed as well. 49 00:02:34,250 --> 00:02:36,873