isalnum() function in C++

In this guide, you will learn what is isalnum() function is in C++ programming and how to use it with an example.

1. isalnum() Function Overview

The isalnum() function in C++ checks if the passed character is either an alphabetic letter or a numeral digit. It is part of the <cctype> header and returns a non-zero value if the character is alphanumeric, and zero otherwise.

Signature:

int isalnum(int ch);

Parameters:

- ch: This is the character to be checked, represented as an int. If ch is a char type, it is internally converted to an int.

2. Source Code Example

#include <iostream>
#include <cctype>

int main() {
    char ch1 = 'A';
    char ch2 = '5';
    char ch3 = '!';

    if(isalnum(ch1)) {
        std::cout << ch1 << " is alphanumeric." << std::endl;
    } else {
        std::cout << ch1 << " is not alphanumeric." << std::endl;
    }

    if(isalnum(ch2)) {
        std::cout << ch2 << " is alphanumeric." << std::endl;
    } else {
        std::cout << ch2 << " is not alphanumeric." << std::endl;
    }

    if(isalnum(ch3)) {
        std::cout << ch3 << " is alphanumeric." << std::endl;
    } else {
        std::cout << ch3 << " is not alphanumeric." << std::endl;
    }

    return 0;
}

Output:

A is alphanumeric.
5 is alphanumeric.
! is not alphanumeric.
Explanation:

1. Three characters, ch1, ch2, and ch3 are defined.


Comments