Activity 5-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.

// P54.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<ctype>
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
        {
            c = tolower(c);
            cout << "Your character " << c << " is in uppercase.";
            cout << "Its lowercase case is " << c << endl;
        }
        else
       {
            c = toupper(c);
            cout << "Your character " << c << " is in lowercase.";
            cout << "Its uppercase is " << c << endl;
        }
     }
     else
     {
            cout << "Your character " << c << " is a digit.\n";
     }

     return 0;
}

Exercise 5.8
Modify the above program such that if one of the whitespaces is entered, it displays a message and tells what the character was.  Call your new program ex58.cpp.