>> DAVID MALAN: Let's now refine this program a bit further.
Wouldn't it be nice if toupper could capitalize a letter if lowercase, and
if not lowercase, pass it through unchanged?
In other words, could I replace my if-else block with a
single call to toupper?
>> Well, to answer this question, I'd best consult the
documentation for toupper.
To do so, let me open a larger terminal window outside of gedit, and
then type man toupper, thereby signifying that I'd like to open the
so-called man page for the function toupper.
Upon hitting Enter, I see a screen like this one.
And now notice that they seem to have combined the documentation for toupper
upper with that for tolower.
But no matter.
>> You'll notice under the synopsis that I'm reminded that, indeed, to use this
function, I must include the header file ctype.h.
Below that, you see that toupper is declared as returning an int and
accepting an int, which is a bit curious, since thus far, I've been
manipulating chars only.
But that's OK.
It turns out that this is just a convention, and we can still use c as
though it's a char, even though it's declared as an int.
>> Now, in the description here, I see that toupper converts the letter c to
uppercase if possible.
And under return value, I see that the value returned is that of the
converted letter, or c, if the conversion was not possible.
And there's the insight I was looking for.
If c is not, in fact, a lowercase letter, it seems that toupper will
simply pass it along unchanged.
>> So let's now return to my code.
And let's now remove the entirety of this conditional block and replace it,
quite simply, with printf %c comma toupper of s bracket i close paren
close paren semicolon, thereby signifying that I'd like to replace %c
with the capitalization of the ith letter in s if the ith
letter in s is lowercase.
Or even if it's not, it will be passed through unchanged.
>> Let's now compile this program with make capitalize2, and run it with
capitalize2, again, typing hello.
And indeed, we get back uppercase.
Let's again type David.
And it, too, is uppercase.