C++ isdigit()

The isdigit() function in C++ checks if the given character is a digit or not.

isdigit() Prototype

int isdigit(int ch);

The isdigit() function checks if ch is a digit or not i.e one of 0,1,2,3,4,5,6,7,8 and 9.

The behaviour of isdigit() is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.

It is defined in <cctype> header file.

isdigit() Parameters

ch: The character to check.

isdigit() Return value

The isdigit() function returns non zero value if ch is a digit, otherwise returns zero.

Example: How isdigit() function works

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char str[] = "hj;pq910js4";

    cout << "The digit in the string are:" << endl;
    for (int i=0; i<strlen(str); i++)
    {
        if (isdigit(str[i]))
            cout << str[i] << " ";
    }

    return 0;
}

When you run the program, the output will be:

The digit in the string are:
9 1 0 4