Using the isdigit(), isalpha(), isalnum() and isxdigit functions C++ program example

 

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 the characters using isdigit(), isalpha(), isalnum() and isxdigit functions in C++ programming

To show: How to use the isdigit(), isalpha(), isalnum() and isxdigit() functions in C++ programming

 

// using isdigit(), isalpha(), isalnum() and isxdigit() functions 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 isdigit(), isalpha(),"<<endl;

cout<<"isalnum(), and isxdigit()"<<endl;

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

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

isdigit('8') ? cout<<"8 is a digit"<<endl : cout<<"8 is not a digit"<<endl;

isdigit('#') ? cout<<"# is a digit"<<endl : cout<<"# is not a digit"<<endl;

 

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

isalpha('A') ? cout<<"A is a letter"<<endl : cout<<"A is not a letter"<<endl;

isalpha('b') ? cout<<"b is a letter"<<endl : cout<<"b is not a letter"<<endl;

isalpha('&') ? cout<<"& is a letter"<<endl : cout<<"& is not a letter"<<endl;

isalpha('4') ? cout<<"4 is a letter"<<endl : cout<<"4 is not a letter"<<endl;

 

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

isalnum('A') ? cout<<"A is a digit or a letter"<<endl : cout<<"A is not a digit or a letter"<<endl;

isalnum('8') ? cout<<"8 is a digit or a letter"<<endl : cout<<"8 is not a digit or a letter"<<endl;

isalnum('#') ? cout<<"# is a digit or a letter"<<endl : cout<<"# is not a digit or a letter"<<endl;

 

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

isxdigit('F') ? cout<<"F is a hexadecimal"<<endl : cout<<"F is not a hexadecimal"<<endl;

isxdigit('J') ? cout<<"J is a hexadecimal"<<endl : cout<<"J is not a hexadecimal"<<endl;

isxdigit('7') ? cout<<"7 is a hexadecimal"<<endl : cout<<"7 is not a hexadecimal"<<endl;

isxdigit('$') ? cout<<"$ is a hexadecimal"<<endl : cout<<"$ is not a hexadecimal"<<endl;

isxdigit('f') ? cout<<"f is a hexadecimal"<<endl : cout<<"f is not a hexadecimal"<<endl;

 

return 0;

}

 

Output example:

 

Using functions isdigit(), isalpha(),

isalnum(), and isxdigit()

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

According to isdigit():

8 is a digit

# is not a digit

 

According to isalpha():

A is a letter

b is a letter

& is not a letter

4 is not a letter

 

According to isalnum():

A is a digit or a letter

8 is a digit or a letter

# is not a digit or a letter

 

According to isxdigit():

F is a hexadecimal

J is not a hexadecimal

7 is a hexadecimal

$ is not a hexadecimal

f is a hexadecimal

Press any key to continue . . .

 

 

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