Activity 8-4 - Predefined Character Functions in cctype (ctype)



In C++, we have several predefined character functions.  These functions are listed below:
 
 
Function Name
Description
Example
toupper(a_character) Returns the uppercase version of the argument character.  If the character is already an uppercase, it will remain unchanged. char c, d = 'a';
c = toupper(d);
cout << c;
Output is:  A
tolower(a_character) Returns the lowercase version of the argument character.  If the character is already a lowercase, it will remain unchanged. char c, d = 'A';
c = tolower(d);
cout << c;
Output is:  a
isupper(a_character) Returns true if the character is an uppercase letter, otherwise it returns false char d = 'A';
cout << isupper(d);
Output is:  1
Note: it can be used in an if statement
islower(a_character) Returns true if the character is a lowercase letter, otherwise it returns false char d = 'A';
cout << islower(d);
Output is:  0
Note: it can be used in an if statement
isalpha(a_character) Returns true if the character is one of the letters of the alphabet, otherwise it returns false char d = 'A';
cout << isalpha(d);
Output is:  1
Note: it can be used in an if statement
isdigit(a_character) Returns true if the character is one of the digits '0' to '9', otherwise it returns false char d = 'A';
cout << isdigit(d);
Output is:  0
Note: it can be used in an if statement
isspace(a_character) Returns true if the character is a whitespace character, blank space, newline, or tab, otherwise it returns false char d = '\n';
cout << isspace(d);
Output is:  1
Note: it can be used in an if statement

The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit.

// P83.cpp - This program reads one character from the keyboard and will
// convert it to uppercase, if it is lowercase
// convert it to lowercase, if it is uppercase
// display a message, if it is a digit

#include<iostream>
#include<cctype>
using namespace std;

int main( )
{
    char c;

    cout << "Enter a character \n";
    cin >> c;

     if(isalpha(c))
    { //check to see if it is a letter of alphabet
        if( isupper(c) ) //check to see if it is uppercase
        {
            cout << "Your character " << c << " is in uppercase.";
            c = tolower(c);
            cout << "Its lowercase case is " << c << endl;
        }
        else
       {
            cout << "Your character " << c << " is in lowercase.";
            c = toupper(c);
            cout << "Its uppercase is " << c << endl;
        }
     }
     else
     {
            cout << "Your character " << c << " is a digit.\n";
     }

     return 0;
}

Exercise 8.8
The above program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. However, the above program skips the whitespaces. Modify the above program so that that if a whitespaces is entered, the program displays a message that indicates that a whitespace has been printed. Call your new program ex88.cpp.