< previous page page_525 next page >

Page 525
Notice that the value returned by each function is just the original character if the condition is not met. For example, tolower(M) returns the character m, whereas tolower(+) returns +.
A common use of these two functions is to let the user respond to certain input prompts by using either uppercase or lowercase letters. For example, if you want to allow either Y or y for a Yes response from the user, and either N or n for No, you might do this:
cout < Enter Y or N: ;
cin >> inputChar;
if (toupper(inputChar) == Y)
{
    .
    .
    .

}
else if (toupper(inputChar) == N)
{
    .
    .
    .

}
else
    PrintErrorMsg();
Below is a function named Lower, which is our implementation of the tolower function. (You wouldn't actually want to waste time by writing this function because tolower is already available to you.) This function returns the lowercase equivalent of an uppercase letter. In ASCII, each lowercase letter is exactly 32 positions beyond the corresponding uppercase letter. And in EBCDIC, the lowercase letters are 64 positions before their corresponding uppercase letters. To make our Lower function work on both ASCII-based and EBCDIC-based machines, we define a constant DISTANCE to have the value
a - A
In ASCII, the value of this expression is 32. In EBCDIC, the value is 64.
#include <ctype.h>    // For isupper()
  .
  .
  .
char Lower( /* in */ char ch )

// Postcondition:

 
< previous page page_525 next page >