0% found this document useful (0 votes)
57 views

Ctype Header File

This document provides examples of functions from the ctype.h header file in C++. It explains the syntax and usage of functions like isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), islower(), isupper(), ispunct(), isxdigit(), and isspace(). For each function, it gives the purpose of the function, provides sample code demonstrating how to use the function to check the type of a character, and displays the output. The examples help illustrate how to use these character classification and conversion functions to determine properties of characters.

Uploaded by

Code Seekers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Ctype Header File

This document provides examples of functions from the ctype.h header file in C++. It explains the syntax and usage of functions like isalnum(), isalpha(), iscntrl(), isdigit(), isgraph(), islower(), isupper(), ispunct(), isxdigit(), and isspace(). For each function, it gives the purpose of the function, provides sample code demonstrating how to use the function to check the type of a character, and displays the output. The examples help illustrate how to use these character classification and conversion functions to determine properties of characters.

Uploaded by

Code Seekers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

[email protected]. https://programcodescpp.wixsite.

com/programcodes

ALL FUNCTIONS
OF ctype.h
HEADER FILE
WITH EXAMPLE
IN C++

By DJVprogrammers

https://web.facebook.com/DJVprogrammers 1
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF isalum() builtin function in C++


• Syntax
o This function is used to check weather the entered character is alphanumeric
(means alphabet of number) or not and returns 1 or 0 according to the condition
o int isalnum(int c);

Code:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
6. char ch;
7. cout<<"\n\tEnter any character : ";
8. cin>>ch;
9. if( isalnum(ch) )
10. cout<<"\n\tCharacter : "<<ch<<" is Alphanumeric"<<endl;
11. else
12. cout<<"\n\tCharacter : "<<ch<<" is Not Alphanumeric"<<endl;
13. return 0;
14. }

https://web.facebook.com/DJVprogrammers 2
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF isalpha() builtin function in C++


• Syntax
o This function is used to check weather the entered character is alphabet or not and
returns 1 or 0 according to the condition.
o int isalpha(int c);

Code:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>

4. int main ()
5. {
a. char ch;
6. cout<<"\n\tEnter any character : ";
7. cin>>ch;

8. if( isalpha(ch) )
9. cout<<"\n\tCharacter : "<<ch<<" is Alphabet"<<endl;
10. else
11. cout<<"\n\tCharacter : "<<ch<<" is Not Alphabet"<<endl;
12. return 0;
13. }

https://web.facebook.com/DJVprogrammers 3
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF iscntrl() builtin function in C++


• Syntax

• 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)

Code:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>

4. int main ()
5. {
6. //example 1
7. string ch ="";
8. if( iscntrl(ch[0]) )
9. cout<<"\n\t"<<ch<<" is Control character"<<endl;
10. else
11. cout<<"\n\t"<<ch<<" is Not Control character"<<endl;
12. //example 2
13. string st="I am programmer ";
14. if( iscntrl(st[0]) )
15. cout<<"\n\t"<<st<<" is Control character"<<endl;
16. else
17. cout<<"\n\t"<<st<<" is Not Control character"<<endl;
18. return 0;
19. }

https://web.facebook.com/DJVprogrammers 4
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF isdigit() FUNCTION in C++

• SYNTAX
o This function is used to check weather the entered character is decimal number (
0,1,2,….,9) or not are returns positive value if true else 0.
o Int isdigit(int c);

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
a. char ch;
6. cout<<"\n\tEnter any character : ";
7. cin>>ch;
8. if( isdigit(ch) )
9. cout<<"\n\tCharacter : "<<ch<<" is Decimal Number"<<endl;
10. else
11. cout<<"\n\tCharacter : "<<ch<<" is Not Decimal Number"<<endl;
12. return 0;
13. }

https://web.facebook.com/DJVprogrammers 5
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF isgraph() and isprint() FUNCTION in C++

• SYNTAX
o Both functions have same functionality
o This function int isgraph(int c) checks if the character has graphical
representation.
o 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.

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main () {
a. char ch='A'; //example 1
5. if( isgraph(ch) || isprint(ch))
6. cout<<"\n\tCharacter : "<<ch<<" can be printed"<<endl;
7. else
8. cout<<"\n\tCharacter : "<<ch<<" can't be printed"<<endl;
9. //example 2
i. ch=' ';
10. if( isgraph(ch) || ispint(ch))
11. cout<<"\n\tCharacter : "<<ch<<" can be printed"<<endl;
12. else
13. cout<<"\n\tCharacter : "<<ch<<" can't be printed"<<endl;
14. return 0;
15. }

https://web.facebook.com/DJVprogrammers 6
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF islower() and isupper() FUNCTION in C++

• SYNTAX
o Int islower(int c);
o 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.
o Int isupper(int c);
o 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.

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
6. char ch;
7. cout<<"\n\tEnter any character : ";
8. cin>>ch;
9. if( islower(ch) )
10. cout<<"\n\tCharacter : "<<ch<<" is lower case"<<endl;
11. else if(isupper(ch))
12. cout<<"\n\tCharacter : "<<ch<<" is upper case"<<endl;
13. else
14. cout<<"\n\tCharacter : "<<ch<<" is not an alphabet"<<endl;
15. return 0;
16. }

https://web.facebook.com/DJVprogrammers 7
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF ispunct() FUNCTION in C++

• SYNTAX
o This function is used to check weather the entered character is from punctuation
characters (! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~) or not and
returns positive value if true else 0.
o Int ispunct(int c);

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
6. char ch;
7. cout<<"\n\tEnter any character : ";
8. cin>>ch;
9. if( ispunct(ch) )
10. cout<<"\n\tCharacter : "<<ch<<" is Punctuation character"<<endl;
11. else
12. cout<<"\n\tCharacter : "<<ch<<" is not Punctuation character"<<endl;
13. return 0;
14. }

https://web.facebook.com/DJVprogrammers 8
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF isxdigit() FUNCTION in C++

• SYNTAX
o This function is used to check weather the entered character is decimal 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.
o Int isxdigit(int c);

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
a. char ch;
6. cout<<"\n\tEnter any character : ";
7. cin>>ch;
8. if( isxdigit(ch) )
9. cout<<"\n\tCharacter : "<<ch<<" is HexDecimal Number"<<endl;
10. else
11. cout<<"\n\tCharacter : "<<ch<<" is Not HexDecimal Number"<<endl;
12. return 0;
13. }

https://web.facebook.com/DJVprogrammers 9
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF isspace() FUNCTION in C++

• SYNTAX
o This function is used to check weather the entered character is white space or not
o Int isspace(int c);

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
6. char ch='h';//example 1
7. if( isspace(ch) )
8. cout<<"\n\tCharacter : "<<ch<<" is white space"<<endl;
9. else
10. cout<<"\n\tCharacter : "<<ch<<" is not whitespace"<<endl;
11. ch='\t';//example 2
12. if( isspace(ch) )
13. cout<<"\n\tCharacter : "<<ch<<" is white space"<<endl;
14. else
15. cout<<"\n\tCharacter : "<<ch<<" is Not whitespace"<<endl;
16. return 0;
17. }

https://web.facebook.com/DJVprogrammers 10
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF tolower() FUNCTION in C++

• SYNTAX
o This function is used convert a letter to lower case
o Int tolower(int c);

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
6. string s;
7. cout<<"\n\tEnter any sting: ";
8. getline(cin,s);
9. cout<<"\n\tLower case string is : ";
10. for(int i=0;s[i]!='\0';i++)
11. cout<<char(tolower(s[i]));
12. cout<<endl;
13. return 0;
14. }

https://web.facebook.com/DJVprogrammers 11
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF toupper() FUNCTION in C++

• SYNTAX
o This function is used convert a letters to upper case
o int toupper (int c);

CODE:
1. #include<iostream>
2. using namespace std;
3. #include <ctype.h>
4. int main ()
5. {
6. string s;
7. cout<<"\n\tEnter any sting: ";
8. getline(cin,s);
9. cout<<"\n\t Upper case string is : ";
10. for(int i=0;s[i]!='\0';i++)
11. cout<<char(toupper(s[i]));
12. cout<<endl;
13. return 0;
14. }

https://web.facebook.com/DJVprogrammers 12
[email protected]. https://programcodescpp.wixsite.com/programcodes

Website:
https://programcodescpp.wixsite.com/programcodes

Email:
[email protected]

Facebook Page:
https://web.facebook.com/DJVprogrammers

YouTube Channel
https://www.youtube.com/channel/UCfizosx-0fkFJ6R-oF6l9-A?view_as=subscriber

https://web.facebook.com/DJVprogrammers 13

You might also like