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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[5] = "g1" ;
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
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[5] = "Gg" ;
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;
}
}
|
Output
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[5] = "Gg" ;
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;
}
}
|
Output
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[5] = "g1" ;
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
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[5] = "g1" ;
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[4] = " \n\t" ;
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[4] = " \n\t" ;
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
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[4] = " \n\t" ;
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[6] = "\t@gf1" ;
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++
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[6] = "\t@gf1" ;
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
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char ch[6] = "\t@gf1" ;
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
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!