>> DAVID J. MALAN: Suppose that I'd like to implement a program that prompts
the user for a string and then proceeds to capitalize their input,
converting any lowercase letters that they type to uppercase.
Well, let's go ahead and implement that program.
>> Let's first include cs50.h followed by include stdio.h.
And so that we can use strlen, let's include string.h.
>> Let's next declare main as int main void and let's now proceed to prompt
the user for a string.
Printf, let's prompt them for some input.
Now let's declare a string--
we'll call it s--
and store in it the result of calling the cs50 library function GetString.
>> Let's now proceed to iterate over each of the characters in s, capitalizing
any lowercase letters that we see.
For int, i get 0.
Let's also declare n as being equal to the string length of s so that we can
iterate from i up until n, the length of s, and on each
iteration increment i.
>> And then inside of this loop, let's first check is the current letter--
the i-th letter of s, so to speak--
a lowercase letter.
If s bracket i is greater than or equal to lowercase a, and it's less
than or equal to lowercase z--
Now if we want to convert a lowercase letter to uppercase, recall first that
in ASCII a lowercase a is 97 and an uppercase A is 65.
Meanwhile, a lowercase b is 98, and an uppercase B is 66.
>> If we continue to look at that pattern, we'll see that the lowercase
letters are always 32 values higher than the uppercase letters.
So if we want to convert from lowercase to uppercase, it should
suffice, really, to subtract 32 from the user's input.
Or more generally, just subtract that difference between a lowercase a and a
capital A.
>> How to express that?
Well, let's do it in code.
Printf, quote, unquote "%c" to print the current character, followed by
printing whatever's in s bracket i minus the result of doing lowercase a
minus uppercase A semicolon.
In other words, this parenthetical expression, little a minus big A, is
going to return to us at the end of the day 32.
But I don't have to remember that it's 32.
I can allow the computer to figure out what the difference between lowercase
a and capital A is.
>> Meanwhile, once I know that difference, I can subtract it from s
bracket i, which will take what's presumably a lowercase letter to a
lower value, namely a value that maps onto an uppercase equivalent.
Let's now save, compile, and run this program.
Make capitalize dot slash capitalized.
And my input will be hello.
And there we have, hello.
>> Now my prompt, admittedly, is a bit ugly, because we've
omitted one bit of printing.
And let's go back and add that.
At the very bottom of this program, I'm very simply, and largely for
aesthetic purpose, going to add printf, quote, unquote backslash n.
Let's resave this file, recompile, rerun.
>> Make capitalize, dot slash capitalize.
Again, for input I'll provide "hello" in all lower case and now hit Enter,
and "hello," much more cleanly printed.