ctype header file in C++

C++ is a widely used programming language. It is considered as the father of all programming languages and that is why it is taught is 90% of the college and universities to all CS/IT/SE graduates. Once you have grip over this language, all other languages are child’s play for you to learn. This Language have a lot of libraries that when explored by students makes their coding journey easy. One of them is ctype header File in C++. In this Article, We will learn about the usage of this library and all its functions.

Table of Contents

  • “ctype” header file Introduction
  • How to Use ctype header file library
  • List of Function with Example.
    • isalum() Function
    • isalpha() Function
    • iscntrl() Function
    • isdigit() Function
    • isgraph and isprint() Functions
    • islower() and isupper() Functions
    • ispunct() Function
    • isxdigit() Function
    • isspace() Function
    • tolower() Function
    • toupper() Function

“ctype” Header File in C++

The “ctype” header file is a part of the C++ Standard Library and provides a set of functions for character classification and manipulation. These functions are essential for tasks such as checking whether a character is alphanumeric, converting characters to uppercase or lowercase, and more. The ctype functions are particularly valuable when dealing with text processing, input validation, and parsing.

How to Use it

This library comes by default with all the Compiler and IDEs used for C++ development. you can Include this library in your project by using following code:

#include<ctype.h>

List of Functions of “ctype” Header file

There are a lot of functions in this header file that are used to deal with character datatype. Let’s Explore them one by one.

1. isalum() Function

  • This function is used to check whether the entered character is alphanumeric (means alphabet of number) or not and returns 1 or 0 according to the condition
  •  int isalnum(int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
char ch;
cout<<"\n\tEnter any character : ";
cin>>ch;
if( isalnum(ch) )
cout<<"\n\tCharacter : "<<ch<<" is Alphanumeric"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is Not Alphanumeric"<<endl;
return 0;
}
ctype header file in C++ isalum() function example
ctype header file. isalum() function example

2. isalpha() Function

  • This function is used to check whether the entered character is alphabet or not and returns 1 or 0 according to the condition.
  •  int isalpha(int c);
#include<iostream>
using namespace std;
#include <ctype.h>

int main () 
{
char ch;
cout<<"\n\tEnter any character : ";
cin>>ch;

if( isalpha(ch) )
cout<<"\n\tCharacter : "<<ch<<" is Alphabet"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is Not Alphabet"<<endl;
return 0;
}

isalpha(char) function Example

3. iscntrl() Function

  • This function checks if the passed character is a control character.
  • According to standard ASCII character set, control characters are between ASCII codes 0x00 (NUL), 0x1f (US), and 0x7f (DEL). Specific compiler implementations for certain platforms may define additional control characters in the extended character set (above 0x7f).
  •  int iscntrl(int c)
#include<iostream>
using namespace std;
#include <ctype.h>

int main () 
{
//example 1
string ch ="";
if( iscntrl(ch[0]) )
cout<<"\n\t"<<ch<<" is Control character"<<endl;
else 
cout<<"\n\t"<<ch<<" is Not Control character"<<endl;
//example 2
string st="I am programmer ";
if( iscntrl(st[0]) )
cout<<"\n\t"<<st<<" is Control character"<<endl;
else 
cout<<"\n\t"<<st<<" is Not Control character"<<endl;
return 0;
}
ctype builtin header file in C++ iscntrl() function example.

4. isdigit() Function

  • This function is used to check whether the entered character is decimal number ( 0,1,2,….,9) or not are returns positive value if true else 0.
  • Int isdigit(int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
char ch;
cout<<"\n\tEnter any character : ";
cin>>ch;
if( isdigit(ch) )
cout<<"\n\tCharacter : "<<ch<<" is Decimal Number"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is Not Decimal Number"<<endl;
return 0;
}
ctype header file in C++ complete guide for beginners, example of isdigit function. How to check if a character is digit or not in C++

5. isgraph() and isprint() Function

  • Both functions have same functionality
  • This function int isgraph(int c) checks if the character has graphical representation.
  • The characters with graphical representations are all those characters that can be printed except for whitespace characters (like ‘ ‘), which is not considered as isgraph characters.
#include<iostream>
using namespace std;
#include <ctype.h>
int main ()  {
char ch='A'; //example 1
if( isgraph(ch) )
cout<<"\n\tCharacter : "<<ch<<" can be printed"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" can't be printed"<<endl;
//example 2
ch=' ';
if( isgraph(ch) )
cout<<"\n\tCharacter : "<<ch<<" can be printed"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" can't be printed"<<endl;
return 0;
}
 Complete ctype header file tutorial for beginners, how to check if user entered some printable character or not in C++.

6. islower() and isupper() Function

  • Int islower(int c);
    • This function is used to check weather the entered character is lower case ( a,b,c,…,z) or not and returns positive value if true else 0.
  • Int isupper(int c);
    • This function is used to check weather the entered character isupper case ( A,B,C,…,Z) or not and returns positive value if true else 0.
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
char ch;
cout<<"\n\tEnter any character : ";
cin>>ch;
if( islower(ch) )
cout<<"\n\tCharacter : "<<ch<<" is lower case"<<endl;
else if(isupper(ch))
cout<<"\n\tCharacter : "<<ch<<" is upper case"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is not an alphabet"<<endl;
return 0;
}
is lower or uppercase letter checking in C++,how to check if a character is lowercase or uppercase in C++.

7. ispunct() Function

  • This function is used to check weather the entered character is from punctuation characters (! ” # $ % & ‘ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~) or not and returns positive value if true else 0.
  • Int ispunct(int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
char ch;
cout<<"\n\tEnter any character : ";
cin>>ch;
if( ispunct(ch) )
cout<<"\n\tCharacter : "<<ch<<" is Punctuation character"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is not Punctuation character"<<endl;
return 0;
}
how to check if a character is punctuation mark or not in C++. ctype header file in C++.

8. isxdigit() Function

  • This function is used to check whether the entered character is hexadecimal number ( 0,1,2,….,9,A,B,C,D,E,F,a,b,c,d,e,f) or not and returns positive value if true else 0.
  • Int isxdigit(int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
char ch;
cout<<"\n\tEnter any character : ";
cin>>ch;
if( isxdigit(ch) )
cout<<"\n\tCharacter : "<<ch<<" is HexDecimal Number"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is Not HexDecimal Number"<<endl;
return 0;
}
check whether the entered character is hexadecimal number or not in C++. ctype header file in C++.

9. isspace() Function

  • This function is used to check whether the entered character is white space or not
  • Int isspace(int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
char ch='h';//example 1
if( isspace(ch) )
cout<<"\n\tCharacter : "<<ch<<" is white space"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is not whitespace"<<endl;
ch='\t';//example 2
if( isspace(ch) )
cout<<"\n\tCharacter : "<<ch<<" is white space"<<endl;
else 
cout<<"\n\tCharacter : "<<ch<<" is Not whitespace"<<endl;
return 0;
}
is white space or not in C++ using cytpe header file.

10. tolower() Function

  • This function is used convert a letter to lower case
  • Int tolower(int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
string s;
cout<<"\n\tEnter any sting: ";
getline(cin,s);
cout<<"\n\tLower case string is : ";
for(int i=0;s[i]!='\0';i++)
cout<<char(tolower(s[i]));
cout<<endl;
return 0;
}
How to convert text to lowercase in C++. cytpe header file in C++.

11. toupper() Function

  • This function is used convert a letters to upper case
  • int toupper (int c);
#include<iostream>
using namespace std;
#include <ctype.h>
int main () 
{
string s;
cout<<"\n\tEnter any sting: ";
getline(cin,s);
cout<<"\n\t Upper case string is : ";
for(int i=0;s[i]!='\0';i++)
cout<<char(toupper(s[i]));
cout<<endl;
return 0;
}
Convert string to upper case using C++. ctype header file in C++.

2 thoughts on “ctype header file in C++”

Leave a Comment