Skip to content
Related Articles

Related Articles

Character Classification in C++ : cctype

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 08 Mar, 2022
Improve Article
Save Article

Character classification in C++ is possible using functions specified in function library. These functions are included in the <cctype> header file.

Numerous functions to classify characters are discussed below:
1. isalpha(): This function returns true if the character is an alphabet else returns false. All the characters from a-z and A-Z return true according to this function.

Syntax:

int isalpha ( char c );

Example:

C++




// C++ program to demonstrate isalpha()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[5] = "g1";
 
    // checking for isalpha() function
    for (int i = 0; i < 2; i++) {
        if (isalpha(ch[i]))
            cout << ch[i] << " is alphabet" << endl;
        else
            cout << ch[i] << " is not alphabet" << endl;
    }
}

Output

g is alphabet
1 is not alphabet

2. isalnum(): This function returns true if the character is an alphabet or a number else returns false. All the characters from a-z, A-Z, and all numbers return true according to this function.

Syntax:

int isalnum ( char c );

Example:

C++




// C++ program to demonstrate isalnum()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[5] = "g1";
 
    // checking for isalnum() function
    for (int i = 0; i < 2; i++) {
        if (isalnum(ch[i]))
            cout << ch[i] << " is alphanumeric" << endl;
        else
            cout << ch[i] << " is not alphanumeric" << endl;
    }
}

Output

g is alphanumeric
1 is alphanumeric

3. isdigit(): This function returns true if the character is a number else returns false. All Numbers return true according to this function.

Syntax:

int isdigit ( char c );

Example:

CPP




// C++ program to demonstrate isdigit()
#include <cctype>
#include <iostream>
using namespace std;
 
int main()
{
    // initializing character array
    char ch[5] = "g1";
 
    // checking for isdigit() function
    for (int i = 0; i < 2; i++) {
        if (isdigit(ch[i]))
            cout << ch[i] << " is digit" << endl;
        else
            cout << ch[i] << " is not digit" << endl;
    }
}

Output

g is not digit
1 is digit

4. isblank(): This function returns true if the character is a space or tab else returns false.

Syntax:

int isblank ( char c );

Example:

C++




// C++ program to demonstrate isblank()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[4] = " \n\t";
 
    // checking for isblank() function
    for (int i = 0; i < 3; i++) {
        if (isblank(ch[i]))
            cout << " Character is blank" << endl;
        else
            cout << " Character is not blank" << endl;
    }
}

Output

 Character is blank
 Character is not blank
 Character is blank

5. isspace(): This function returns true if the character is a space or tab or whitespace control code ( Eg.\n,\r ) else returns false.

Syntax:

int isspace ( char c );

Example:

C++




// C++ program to demonstrate isspace()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[4] = " \n\t";
 
    // checking for isspace() function
    for (int i = 0; i < 3; i++) {
        if (isspace(ch[i]))
            cout << " Character is space" << endl;
        else
            cout << " Character is not space" << endl;
    }
}

Output

 Character is space
 Character is space
 Character is space

6. iscntrl(): This function returns true if the character is tab or any control code else returns false.

Syntax:

int iscntrl ( char c );

Example:

CPP




// C++ program to demonstrate iscntrl()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[4] = " \n\t";
 
    // checking for iscntrl() function
    for (int i = 0; i < 3; i++) {
        if (iscntrl(ch[i]))
            cout << " Character is control code " << endl;
        else
            cout << " Character is not control code"
                 << endl;
    }
}

Output

 Character is not control code
 Character is control code 
 Character is control code 

7. isprint(): This function returns true if the character is printable on the console i.e. all characters except control code else return false.

Syntax:

int isprint ( char c );

Example:

C++




// C++ program to demonstrate isprint()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[6] = "\t@gf1";
 
    // checking for isprint() function
    for (int i = 0; i < 5; i++) {
        if (isprint(ch[i]))
            cout << ch[i] << " is printable character "
                 << endl;
        else
            cout << ch[i] << " is not printable Character"
                 << endl;
    }
}

Output

     is not printable Character
@ is printable character 
g is printable character 
f is printable character 
1 is printable character 

8. isxdigit(): This function returns true if character is hexadecimal i.e 0-9 and a-f else returns false.

Syntax:

int isxdigit ( char c );

Example:

C++




// C++ program to demonstrate isxdigit()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[6] = "\t@gf1";
 
    // checking for isxdigit() function
    for (int i = 0; i < 5; i++) {
        if (isxdigit(ch[i]))
            cout << ch[i] << " is hexadecimal Character"
                 << endl;
        else
            cout << ch[i] << " is not hexadecimal Character"
                 << endl;
    }
}

Output

     is not hexadecimal Character
@ is not hexadecimal Character
g is not hexadecimal Character
f is hexadecimal Character
1 is hexadecimal Character

9. ispunct(): This function returns true if the character is punctuation mark else returns false.

Syntax:

int ispunct ( char c );

Example:

CPP




// C++ program to demonstrate ispunct()
#include <cctype>
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // initializing character array
    char ch[6] = "\t@gf1";
 
    // checking for ispunct() function
    for (int i = 0; i < 5; i++) {
        if (ispunct(ch[i]))
            cout << ch[i] << " is punctuation mark" << endl;
        else
            cout << ch[i] << " is not punctuation mark"
                 << endl;
    }
}

Output

     is not punctuation mark
@ is punctuation mark
g is not punctuation mark
f is not punctuation mark
1 is not punctuation mark

This article is contributed by Manjeet Singh(S.Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!