C++ Character Classification And Tranformation Functions

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This Tutorial Explains C++ Character & Transformation Functions with Examples. Some Important Functions Covered are isdigit, islower, isupper, isalpha etc.:

In our previous tutorial on “library functions”, we saw the various headers that provide numerous functions that operate on different data types. The header <cctype.h> is one such header that contains a set of functions that classify and manipulate individual characters.

=> Visit Here To Learn C++ From Scratch.

The functions declared in <cctype.h> header, take a single character (int equivalent) as a parameter and return an integer value that can either be a Boolean value or a character value. A value 0 (zero) indicates false while a value other than 0 (non-zero) indicates true.

C++ Character functions

These single character functions can be used in the program by including the header <cctype.h>. These functions are widely used and accepted and are extremely useful in C++ programming for testing and transforming or manipulating characters.

In this tutorial, we will discuss the various character functions and see how each of these functions can be used in a program.

C++ Character Functions

C++ character functions are the functions that take only a single character as a parameter (casted to int) and return a result. These can be classification functions like isalpha, isalnum, isdigit, islower, isupper, etc. to name a few and transforming functions like toupper and tolower that transform a given character into uppercase or lowercase respectively.

In the header <cctype.h>, we have two types of functions defined as stated below.

(i) Character Classification Functions

Character classification functions are used to check if the single character passed as an argument to the function belongs to a certain category.

We have tabularized the character classification functions included in <cctype.h> header below:

NoFunction nameFunction Purpose
1isalnumChecks if a given character is alphanumeric
2isalphaChecks if the character is alphabetic
3isblankChecks if the character is blank
4iscntrlChecks if the given character is a control character
5isdigitChecks if the given character is a decimal digit
6isgraphChecks if the given character has a graphical representation
7islowerChecks if a given character is lowercase
8isprintChecks if the character is a printable character
9ispunctChecks if the character is a punctuation character
10isspaceChecks if the character is a white-space
11isupperChecks if the given character is uppercase
12isxdigitChecks if the character hexadecimal digit

(ii) Character Transforming Functions

Character transforming functions convert between cases. The following functions are included in <cctype.h> file.

NoFunction NameFunction purpose
1tolowerConverts uppercase character to lowercase
2toupperConverts lowercase character to uppercase

Let’s see each of the character functions (classification and transforming) in detail. We will also present a simple C++ programming example for each of the functions.

Isalnum

Function Prototype: int isalnum(int c);

Parameter(s): c=> A character which should be checked if alphanumeric or not

Return Value:

non-zero => c is alphanumeric

0 => c is not alphanumeric

Description: This function checks if a given character c is alphanumeric or not. By alphanumeric, it means a character is between A(a)-Z(z) or 0 to 9.

In C++, this function is locale-specific and its definition is present in header <locale>.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
   char chary[12] = "Hello World"; 
  
    // checking for isalnum() function 
    for (int i=0; i<12; i++) 
    { 
        if (isalnum(chary [i])) 
            cout << chary [i] << " is alphanumeric" << endl; 
        else
            cout << chary [i]  << " is not alphanumeric" << endl; 
    } 
   cout << endl; 
} 

Output:

Isalnum output

Isalpha

Function Prototype: int isalpha(int c);

Parameter(s): c-> Character that is to be checked if alphabetic or not.

Return Value:

non-zero => c is alphabetic

0 => not alphabetic

Description: This function checks if a given character c is alphabetic character i.e. among lowercase and uppercase letters. Like isalnum this function also depends on the locale that we are using.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[5] = "sth1"; 
    for (int i=0; i<5; i++) { 
        if (isalpha(ch_alpha[i])) 
            cout << ch_alpha[i] << " is an alphabet" << endl; 
        else
            cout << ch_alpha[i] << " is not an alphabet" << endl; 
    } 
    cout << endl; 
}

Output:

Isalpha output

Isblank

Function Prototype: int isblank(int c);

Parameter(s): c=> Character which is to be checked.

Return Value:

non-zero => character is blank

0 => character is not blank

Description: This function checks if the given character is a blank or not. Blank character is a space character that is used to separate words in a line of text.

In C++, the locale-specific version of isblank function exists in <locale>.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[6] = "s t h"; 
  
    for (int i=0; i<6; i++) 
    { 
        if (isblank(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a blank" << endl; 
        else
            cout << ch_alpha[i] << " is not a blank" << endl; 
    } 
    cout << endl; 
}

Output:

Isblank output

Note the last line in the output which says “ is not blank”. This is because of the character array’s last position that is not initialized to a value but still has a printing position. Hence it is not a blank.

Iscntrl

Function Prototype: int iscntrl(int c);

Parameter(s): c=> Character to be checked

Return Value:

non-zero=> c is control character

0=> c is not control character

Description: Function checks if the given parameter is a control character. A control character is a character that does not appear in the display or does not occupy a printing position.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[6] = "s\nt\th"; 
  
    for (int i=0; i<6; i++) 
    { 
        if (iscntrl(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a control character" << endl; 
        else
            cout << ch_alpha[i] << " is not a control character" << endl; 
    } 
    cout << endl; 
}

Output:

Iscntrl

Isdigit

Function Prototype: int isdigit(int c)

Parameter(s): c=> A character casted to int whose value is to be checked.

Return Value: non-zero => c is decimal digit

0=> c is not a digit

Description: This function checks if the given parameter is a decimal digit or not. A character is a decimal digit if its value is between 0 and 9.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[5] = "a12b"; 
  
    for (int i=0; i<5; i++) 
    { 
        if (isdigit(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a digit" << endl; 
        else
            cout << ch_alpha[i] << " is not a digit" << endl; 
    } 
    cout << endl; 
}

Output:

Isdigit

Isgraph

Function Prototype: int isgraph(int c)

Parameter(s): c=> Character casted int that is checked using the isgraph function

Return Value:

non-zero=> c is a graphical representation

0=> c is not a graphical representation

Description: Function checks if the given character c is a character with graphical representation. A character that can be printed is a character with graphical representation.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[6] = "a~12b"; 
  
    for (int i=0; i<6; i++) 
    { 
        if (isgraph(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a graphical character" << endl; 
        else
            cout << ch_alpha[i] << " is not a graphical character" << endl; 
    } 
    cout << endl; 
}

Output:

Isgraph

Islower

Function Prototype: int islower(int c)

Parameter(s): c=> Character that is to be checked using the islower function.

Return Value:

non-zer0=> c is lowercase

0=> c is not lowercase

Description: The function islower checks if the given character c is lowercase or not.

A lowercase character is the one that has one of the values: a b c d e f g h I j k l m n o p q r s t u v w x y z.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[4] = "Sth"; 
  
    for (int i=0; i<4; i++) 
    { 
        if (islower(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a lowercase character" << endl; 
        else
            cout << ch_alpha[i] << " is not a lowercase character" << endl; 
    } 
    cout << endl; 
}

Output:

Islower

Isprint

Function Prototype: int isprint(int c)

Parameter(s): c=> Character that is to be checked using isprint function.

Return Value:

non-zer0=> c is printable

0=> c is not printable

Description: Function isprint checks if the given character is printable. A printable character is the one that occupies a printing position in the display.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[4] = "Sth"; 
  
    for (int i=0; i<4; i++) 
    { 
        if (isprint(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a printable character" << endl; 
        else
            cout << ch_alpha[i] << " is not a printable character" << endl; 
    } 
    cout << endl; 
}

Output:

Isprint

Ispunct

Function Prototype: int ispunct(int c)

Parameter(s): c=> Character that is to be checked using the ispunct function.

Return Value:

non-zer0=> c is a punctuation character

0=> c is not punctuation character

Description: This function checks if the given character is a punctuation character. A character that is a graphical character (from isgraph) and not an alphanumeric (from isalnum) is said to be a punctuation character.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[8] = "sth@123"; 
  
    for (int i=0; i<8; i++) 
    { 
        if (ispunct(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a punctuation character" << endl; 
        else
            cout << ch_alpha[i] << " is not a punctuation character" << endl; 
    } 
    cout << endl; 
}

Output:

Ispunct

Isspace

Function Prototype: int isspace(int c)

Parameter(s): c=> Character that is to be checked using the isspace function.

Return Value:

non-zer0=> c is a white-space

0=> c is not white-space

Description: This function checks if the given character is a white space. For the “C” locale, white-space characters are any of the following.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[7] = "s\tt\rh"; 
  
    for (int i=0; i<7; i++) 
    { 
        if (isspace(ch_alpha[i])) 
            cout << ch_alpha[i] << " is a white space" << endl; 
        else
            cout << ch_alpha[i] << " is not a white space" << endl; 
    } 
    cout << endl; 
}

Output:

Isspace

Isupper

Function Prototype: int isupper(int c)

Parameter(s): c=> Character that is to be checked using isupper function.

Return Value:

non-zer0=> c is uppercase

0=> c is not uppercase

Description: The functions islower checks if the given character c is uppercase or not.

An uppercase character is the one that has one of the values: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[4] = "STh"; 
  
    for (int i=0; i<4; i++) 
    { 
        if (isupper(ch_alpha[i])) 
            cout << ch_alpha[i] << " is an uppercase character" << endl; 
        else
            cout << ch_alpha[i] << " is not an uppercase character" << endl; 
    } 
    cout << endl; 
}

Output:

Isupper

Isxdigit

Function Prototype: int isxdigit(int c)

Parameter(s): c=> Character that is to be checked using isxdigit function.

Return Value:

non-zer0=> c is hexadecimal

0=> c is not a hexadecimal

Description: This function checks if the given character is a hexadecimal digit. Valid hexadecimal digits are 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[6] = "sbc10"; 
  
    for (int i=0; i<6; i++) 
    { 
        if (isxdigit(ch_alpha[i])) 
            cout << ch_alpha[i] << " is hexadecimal digit" << endl; 
        else
            cout << ch_alpha[i] << " is not hexadecimal digit" << endl; 
    } 
    cout << endl; 
}

Output:

Isxdigit

tolower

Function Prototype: int tolower(int c)

Parameter(s): c=> Character that is to be converted to lowercase.

Return Value:

returns the lower equivalent of c, unchanged otherwise.

The return value is int and can be implicitly cast to char.

Description: This converts the given character c into its lowercase equivalent. If the conversion is not possible for a given character, then the unchanged value (c) is returned.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[4] = "STH"; 
  
    cout<<ch_alpha<<" converted to lowercase:";
    for (int i=0; i<4; i++) 
    { 
        cout<<(char)tolower(ch_alpha[i]);
    } 
    cout << endl; 
}

Output:

STH converted to lowercase:sth

toupper

Function Prototype: int toupper(int c)

Parameter(s): c=>character that is to be converted to uppercase

Return Value:

returns the upper equivalent of c, unchanged otherwise.

The return value is int and can be implicitly cast to char.

Description: This converts the given character c into its uppercase equivalent. If a conversion is not possible for a given character, then the unchanged value (c) is returned.

Example:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch_alpha[10] = "tutorials"; 
  
    cout<<ch_alpha<<" converted to lowercase:";
    for (int i=0; i<10; i++) 
    { 
        cout<<(char)toupper(ch_alpha[i]);
    } 
    cout << endl; 
}

Output:

tutorials converted to lowercase: TUTORIALS

Conclusion

This ends our tutorial on Character functions in C++.

In this tutorial, we have seen various functions that take a single character and then either classify them or convert them. In our subsequent tutorials, we will discuss string and their functions and classes related to C++.

=> Discover The Simple C++ Training Series Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment