Character Classification in C++ : cctype

Character classification in C++ is possible using functions specified in function library . Numerous function to classify characters are discussed in this article.

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

2. isalnum() : This function returns true if 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.

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

// C++ program to demonstrate insalpha(), isnum() 
// and isdigit()
#include <cctype>
#include <iostream>
using namespace std;

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;
    }

    cout << endl;

    // 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;
    }

    cout << endl;

    // 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 alphabet
1 is not alphabet

g is alphanumeric
1 is alphanumeric

g is not digit
1 is digit

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

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

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

// C++ program to demonstrate iscntrl(), isblank() and
// isspace()
#include <cctype>
#include <iostream>
using namespace std;

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;
    }

    cout << endl;

    // 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;
    }

    cout << endl;

    // 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 not control code
 Character is control code 
 Character is control code 

 Character is blank
 Character is not blank
 Character is blank

 Character is space
 Character is space
 Character is space

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

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

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

// C++ program to demonstrate isprint(), isxdigit() and
// ispunct()
#include <cctype>
#include <iostream>
using namespace std;

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;
    }

    cout << endl;

    // 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;
    }

    cout << endl;

    // 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 printable Character
@ is printable character 
g is printable character 
f is printable character 
1 is printable character 

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

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

Reference : http://www.cplusplus.com/reference/cctype/

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 contribute.geeksforgeeks.org or mail your article to contribute@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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.