- So much goes on with characters that the C library
offers a host of character manipulation functions.
In this movie, I introduce a few of those functions
and show you how to use them in your code.
As a bonus, I offer an introduction to logical expressions,
which can also come into play when working with characters.
In your C code, characters appear either individually
or marching along in an array called a string.
So you can manipulate characters one at a time,
or, more commonly, in the string.
This code includes a ctype header file
which prototypes the character testing
and manipulation functions.
At lines eight and nine, you see
the 'toupper' and 'tolower' functions.
The 'toupper' function generates
the uppercase version of a letter.
'Tolower' generates the lowercase.
The results are used immediately in the printf functions.
Note that these functions require integer values,
not characters.
So you can display the result
by using %c in the printf function
even though they are integer variables.
Build and run the code.
Big a, little a, what begins with a?
As you can see by the output
of the final printf statement in line 10,
the 'toupper' and 'tolower' functions
did not affect the variable ch.
They merely generate an upper- or lowercase equivalent.
You can use these functions on a string, as well.
To do that, you must dissect the string in a loop.
Open exercise ctype2.
Again, an integer variable
is used in the 'toupper' function.
Input is processed and converted to uppercase
at line 11, then output.
You won't see the output until you press the Enter key,
because the C language is stream-oriented.
Build and run the code.
Type some text.
Press the Enter key.
The output is all caps.
But note how the punctuation symbols and spaces
are unaffected by the 'toupper' function.
Most of the functions defined in the ctype header file
are testing functions.
They're used to evaluate the type of character,
such as whether it's a letter, number, space,
or upper or lower case.
These functions return true and false values,
which you can use in your code to see what's been typed.
As an example, exercise ctype3 uses the 'isalpha' function
to determine whether a character
is a letter of the alphabet or some other character.
The function returns 'true' if it is, 'false' otherwise.
In the 'do while' loop, input is received and checked.
The if statement at line 11 returns 'true'
if the character is a letter of the alphabet.
If so, it's displayed.
Build and run.
Type a bunch of text, including symbols and numbers.
Press the Enter key, and only the alphabet characters,
upper- and lowercase, are output.
You can modify the source code for this exercise
to try out many of the character manipulation functions.
For example, change 'isalpha' in line 11 to 'isupper.'
Now only uppercase letters are output.
Save.
Build and run to test the change.
Other functions to find in the ctype header file include
'islower' to detect only lowercase letters,
'isnumber,' which returns 'true' for any number character,
zero through nine,
'isblank' for any white space character,
the space, tab, or new line.
You can also craft your own character manipulation functions
but it helps to know about C logical operators.
The C language's logical operators
are used to compare two or more expressions.
The operators determine whether the result
is true or false.
Three operators are available to use,
logical and, logical or, and logical not.
These are often written in all caps,
so that they're not confused with the regular words.
For an AND operation to be true,
both conditions must be true.
This AND that.
For an OR operator, either of the conditions
must be true.
This OR that.
And for the NOT operator, the condition must be false,
which is backwards, but that's how NOT works.
NOT this.
As an example, the AND operator evaluates two conditions.
Here, both conditions are true.
Therefore, the if statement is executed.
Here, one of the conditions is false.
Therefore, the if statements are skipped.
In the logical OR operator, only one of the conditions
needs to be true.
Here, the if statement would be executed.
When both conditions are false,
the if statement won't be executed.
And in the case of NOT, it negates the condition.
Here, the condition is false.
Variable a equals 50.
But the NOT operator turns that around
and makes it true.
Finally, in the C language, all values are considered true
except zero, which is false.
Typically, the value one is used for true.
But it could also be negative one or any non-zero value.
As an example of constructing your own character test,
open exercise ctype4.
This code is a re-write of exercise ctype3,
where I've substituted the 'iscaps' function for 'isupper.'
A logical comparison is made at line 22.
To understand how it works, read the condition.
If the value of variable ch is less than a,
or the value of variable ch is greater than z.
So when either of those conditions is false,
meaning the character is not uppercase,
the function returns 'false.'
Otherwise, the function returns 'true.'
Suppose you wanted to change the 'iscaps' function
to 'islowercase' and have it return true or false
based on that condition.
How would you go about doing it?
Well, the first thing to do is to change
the name of the function to something more accurate.
There, in the prototype, and here, as well.
And also in the call.
Change the big a to little a
and big z to little z.
Save.
Build and run.
And only the lowercase letters appear.
You can create just about any character test
by using logical operators.
You'll see additional examples
of using logical operators in other movies
in this course.