The C++ standard library cctype contains a number of functions that are useful when working with characters . Two of these functions convert the case of a letter, either from lower to upper or vice versa. The other functions are used to test the value of a character.
The toupper and tolower functions each have one argument, a character. If the character represents a letter, A through Z or a through z, the toupper function returns the upper case of that letter. Conversely, the tolower function returns the lower case of that letter. If the argument is not a letter, both functions simply return the same character that is the argument.
The toupper and tolower functions can be useful. For example, the following program from a previous section of this chapter requires that you check, both in an if condition and a while condition, for an uppercase Q and a lowercase q to determine if the user elected to quit:
#include <iostream> using namespace std; int main(void) { char ch; do { cout << "Press Q or q to quit, any other key to continue: "; cin.get(ch); if (ch != '\n') cin.ignore(); if (ch != 'Q' && ch != 'q') cout << "You want to continue?\n"; else cout << "You quit"; } while (ch != 'Q' && ch != 'q'); return 0; }
The use of the toupper function eliminates the need to check for the lowercase q:
#include <iostream> #include <cctype> using namespace std; int main(void) { char ch; do { cout << "Press Q or q to quit, any other key to continue: "; cin.get(ch); ch = toupper(ch); if (ch != '\n') cin.ignore(); if (ch != 'Q') cout << "You want to continue?\n"; else cout << "You quit"; } while (ch != 'Q'); return 0; }
Pressing the lowercase q still will cause the program to recognize that the user quit even though the if condition and the while condition only compare to an uppercase Q:
Press Q or q to quit, any other key to continue: q You quit
The first modification was to include the cctype standard library in addition to the iostream standard library. This is because the toupper and tolower functions are not defined in the iostream standard library but instead in the cctype standard library.
The other modification is to call the toupper function:
ch = toupper(ch);
The call of the toupper function passes as an argument the character variable into which the user s input was stored. The return value of the function is assigned to the same character value.
It is important to use the return value. The toupper function does not change the value of its argument from lower- to uppercase. Rather, it returns the uppercase version of the argument if the argument is a letter.
Note | This program also could have employed the tolower function instead of the toupper function and then compared it to a lower case q. |
A number of functions in the cctype standard library permit you to determine the value of a character ”that is, whether it is a letter, digit, or whitespace. These functions each take one argument, a character, and return a Boolean value, true or false. Table 12-1 summarizes these functions.
Function | Description |
---|---|
isalpha | Returns true if the argument is a letter of the alphabet; false if otherwise . |
isalnum | Returns true if the argument is a letter of the alphabet or a digit; false if otherwise. |
isdigit | Returns true if the argument is a digit; false if otherwise. |
islower | Returns true if the argument is a lower case letter of the alphabet, false otherwise. |
isprint | Returns true if the argument is a printable character (including a space created by pressing the spacebar ); false if otherwise. |
ispunct | Returns true if the argument is a punctuation character (printable character other than a letter, digit or space); false if otherwise. |
isupper | Returns true if the argument is an uppercase letter of the alphabet; false if otherwise. |
isspace | Returns true if the argument is a whitespace character (tab, newline, or space); false if otherwise. |
You can use these functions to validate user input. For example, the following code fragment illustrates use of a function to validate a three letter password which, to be valid, must consist of an uppercase letter followed by a digit followed by a lowercase letter, such as Z3s.
bool isValidPassWord(char* pw) { if (!isupper(pw[0]) return false; if (!isdigit(pw[1]) return false; if (!islower(pw[2]) return false; return true; }