Character Classification in C++ : cctype
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; } } |
g is alphabet 1 is not alphabet
There comes two functions in the alphabet .
a. isupper(): This function returns true if the character is an upper alphabet else returns false. All the characters from A-Z return true according to this function.
Syntax:
int isupper(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] = "Gg" ; // checking for isupper() function for ( int i = 0; i < 2; i++) { if ( isupper (ch[i])) cout << ch[i] << " is an upper alphabet" << endl; else cout << ch[i] << " is not an upper alphabet" << endl; } } |
G is an upper alphabet g is not an upper alphabet
b. islower(): This function returns true if the character is a lower alphabet else returns false. All the characters from a-z return true according to this function.
Syntax:
int islower(char c);
C++
// C++ program to demonstrate isalpha() #include <cctype> #include <iostream> using namespace std; // Driver Code int main() { // initializing character array char ch[5] = "Gg" ; // checking for islower() function for ( int i = 0; i < 2; i++) { if ( islower (ch[i])) cout << ch[i] << " is an lower alphabet" << endl; else cout << ch[i] << " is not an lower alphabet" << endl; } } |
G is not an lower alphabet g is an lower 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; } } |
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; } } |
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; } } |
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; } } |
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; } } |
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; } } |
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; } } |
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; } } |
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.
Please Login to comment...