C++ Character Functions

C++ character functions are, as the name suggests, functions that work on individual characters. These functions are provided as part of the ‘cctype’ header file. They either test the properties of a character, for example, check whether it is lower case, or, convert a character, either to upper or lower case. Below is a table outlining the character functions that are available. All the functions result in a Boolean ‘true’ or ‘false’ being returned.

Function Description
isalpha Check if a character is a letter of the alphabet.
isalnum Check if a character is numeric or a letter of the alphabet.
isdigit Check if a character is numeric.
islower Check if a character is lower case.
isprint Check if a character is printable.
ispunct Check if a character is punctuation.
isupper Check if a character is upper case.
isspace Check if a character is a space.
tolower Convert a character to lower case.
toupper Convert a character to upper case.

Below is an example of some of these functions in action. A range based ‘for’ loop is used to iterate through the string variable, named ‘example’, one character at a time. Within this loop a check is made to see if the character in question is a letter of the alphabet. If it is, a further check is made to see if the character is lower case. If the character is not a letter of the alphabet then checks are made to see if it is a space or a punctuation character. Feedback is given in the console as a result of the checks made.

string example = "This is a string.";

// Loop through the characters in the string.
for (char character: example)
{

    // Check if the character is a letter of the alphabet.
    if (isalpha(character))
    {
        
        // Check if a character is lower case.
        if (islower(character))
        {
            cout << "The letter '" << character << "' is lower case." << endl;
        }
        else
        {
            cout << "The letter '" << character << "' is not lower case." << endl;
        }
        
    } 
    // Check if a character is a space.
    else if (isspace(character))
    {
        
        cout << "The character '" << character << "' is a space." << endl;
        
    }
    // Check if a character is punctuation.
    else if (ispunct(character))
    {
        
        cout << "The character '" << character << "' is punctuation." << endl;
        
    }

}

The output from the above is shown below.

The letter 'T' is not lower case.
The letter 'h' is lower case.
The letter 'i' is lower case.
The letter 's' is lower case.
The character ' ' is a space.
The letter 'i' is lower case.
The letter 's' is lower case.
The character ' ' is a space.
The letter 'a' is lower case.
The character ' ' is a space.
The letter 's' is lower case.
The letter 't' is lower case.
The letter 'r' is lower case.
The letter 'i' is lower case.
The letter 'n' is lower case.
The letter 'g' is lower case.
The character '.' is punctuation.

A second example here shows one of the conversion functions, ‘toupper’, in action. Again a range based ‘for’ loop is used to iterate through the string. Firstly, the ‘isalpha’ function is used to determine if the character is a letter of the alphabet. If true, the character is then tested to see if it is lower case and converted to upper case if it is, otherwise it is just displayed. Each character is displayed on a separate line in the console. Note that, to ensure that the result of the ‘toupper’ function is a character, a ‘static_cast’ is used. A ‘static_cast’ is used to convert data from one type to another.

string example = "This is a string.";

// Loop through the characters in the string.
for (char character: example)
{

    // Check if the character is a letter of the alphabet.
    if (isalpha(character))
    {
        
        // Check if a character is lower case.
        if (islower(character))
        {
            // Convert to upper case and display.
            cout << static_cast(toupper(character)) << endl;
        }
        else
        {
            // Display characters already in upper case.
            cout << character << endl;
        }
        
    }
    else
    {
        // Display non-alphabet characters.
        cout << character << endl;
    }  

}

The resulting output can be seen here.

T
H
I
S

I
S

A

S
T
R
I
N
G
.