Using the isspace(), iscntrl(), ispunct(), isgraph() and isprint() functions C++ code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Testing characters using isspace(), iscntrl(), ispunct(), isgraph() and isprint() functions in C++ programming

To show: How to use the isspace(), iscntrl(), ispunct(), isprint() and isgraph() functions in C++ programming

 

// using functions isspace(), iscntrl(), ispunct(), isprint() and isgraph() example, for C++ string

// character & string manipulation it is better to use the Standard Template Library, STL

#include <iostream>

using namespace std;

 

int main(void)

{

cout<<"using functions isspace(), iscntrl(),"<<endl;

cout<<"ispunct(), isprint(), isgraph()"<<endl;

cout<<"-------------------------------------"<<endl;

cout<<"According to isspace(): "<<endl;

isspace('\n') ? cout<<"Newline is a whitespace character"<<endl :

cout<<"Newline is not a whitespace character"<<endl;

isspace('\t') ? cout<<"Horizontal tab is a whitespace character"<<endl :

cout<<"Horizontal tab is not a whitespace character"<<endl;

isspace('%') ? cout<<"% is a whitespace character"<<endl :

cout<<"% is not a whitespace character"<<endl;

cout<<"\nAccording to iscntrl(): "<<endl;

iscntrl('\n') ? cout<<"Newline is a control character"<<endl :

cout<<"Newline is not a control character"<<endl;

iscntrl('$') ? cout<<"$ is a control character"<<endl :

cout<<"$ is not a control character"<<endl;

cout<<"\nAccording to ispunct(): "<<endl;

ispunct('y') ? cout<<"y is a punctuation character"<<endl :

cout<<"y is not a punctuation character"<<endl;

ispunct('\'') ? cout<<"\' is a punctuation character"<<endl :

cout<<"\' is not a punctuation character"<<endl;

ispunct('"') ? cout<<"\" is a punctuation character"<<endl :

cout<<"\" is not a punctuation character"<<endl;

cout<<"\nAccording to isprint(): "<<endl;

isprint('$') ? cout<<"$ is a printing character"<<endl :

cout<<"$ is not a printing character"<<endl;

isprint('\a') ? cout<<"Alert is a printing character"<<endl :

cout<<"Alert is not a printing character"<<endl;

cout<<"\nAccording to isgraph(): "<<endl;

isgraph('Q') ? cout<<"Q is a printing character other than a space"<<endl :

cout<<"Q is not a printing character other than a space"<<endl;

isgraph(' ') ? cout<<"Space is a printing character other than a space"<<endl:

cout<<"Space is not a printing character other than a space"<<endl;

return 0;

}

 

Output example:

 

using functions isspace(), iscntrl(),

ispunct(), isprint(), isgraph()

-------------------------------------

According to isspace():

Newline is a whitespace character

Horizontal tab is a whitespace character

% is not a whitespace character

 

According to iscntrl():

Newline is a control character

$ is not a control character

 

According to ispunct():

y is not a punctuation character

' is a punctuation character

" is a punctuation character

 

According to isprint():

$ is a printing character

Alert is not a printing character

 

According to isgraph():

Q is a printing character other than a space

Space is not a printing character other than a space

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index