isblank() function in C++

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

1. isblank() Function Overview

The isblank() function in C++ checks if the passed character is a blank character, which can be either a space or a tab. It is a part of the <cctype> header. The function returns a non-zero value if the character is a blank; otherwise, it returns zero.

Signature:

int isblank(int ch);

Parameters:

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

2. Source Code Example

#include <iostream>
#include <cctype>

int main() {
    // Define three characters for testing
    char ch1 = ' ';
    char ch2 = '\t';
    char ch3 = 'A';

    // Check if ch1 is a blank character and display the result
    if(isblank(ch1)) {
        std::cout << "The first character is a blank." << std::endl;
    } else {
        std::cout << "The first character is not a blank." << std::endl;
    }

    // Check if ch2 is a blank character and display the result
    if(isblank(ch2)) {
        std::cout << "The second character is a blank." << std::endl;
    } else {
        std::cout << "The second character is not a blank." << std::endl;
    }

    // Check if ch3 is a blank character and display the result
    if(isblank(ch3)) {
        std::cout << "The third character is a blank." << std::endl;
    } else {
        std::cout << "The third character is not a blank." << std::endl;
    }

    return 0;
}

Output:

The first character is a blank.
The second character is a blank.
The third character is not a blank.

3. Explanation

In the given code, the isblank() function is used to check whether each of the characters (ch1, ch2, ch3) are blank characters. Both space and tab are recognized as blank characters, but the letter 'A' is not.


Comments