Функция isdigit() в С++
В этом руководстве мы узнаем о функции isdigit() C++ с помощью примеров.
Функция isdigit() в С++ проверяет, является ли данный символ цифрой или нет.
#include using namespace std; int main() < // checks if '9' is a digit cout // Output: 1
Синтаксис
Синтаксис функции isdigit():
isdigit(int ch);
Здесь ch – это символ, который мы хотим проверить.
Параметры
Функция принимает следующие параметры:
- ch – проверяемый символ, приведенный к типу int или EOF
Возвращаемое значение
Функция isdigit() возвращает:
- ненулевое целочисленное значение (истина), если ch – цифра
- целое число ноль (ложь), если ch не является цифрой
Прототип isdigit(), как определено в заголовочном файле cctype:
int isdigit(int ch);
Как мы видим, символьный параметр ch на самом деле имеет тип int. Это означает, что функция isdigit() проверяет значение ASCII символа.
Поведение isdigit() не определено, если:
- значение ch не может быть представлено как unsigned char, или
- значение ch не равно EOF.
Пример: C ++ isdigit()
#include #include #include using namespace std; int main() < char str[] = "hj;pq910js4"; int check; cout return 0; >
The digit in the string are: 9 1 0 4
Здесь мы создали строку C str . Затем мы напечатали только цифры в строке, используя цикл for. Цикл выполняется от i = 0 до i = strlen (str) – 1.
for (int i = 0; i
Другими словами, цикл проходит по всей строке, поскольку strlen() дает длину str .
Функция rint() в C++
На каждой итерации цикла мы используем функцию isdigit(), чтобы проверить, является ли строковый элемент str [i] цифрой или нет. Результат сохраняется в проверочной переменной.
check = isdigit(str[i]);
Если проверка возвращает ненулевое значение, мы печатаем строковый элемент.
C Language: isdigit function
(Test for Digit)
In the C Programming Language, the isdigit function tests whether c is a digit.
Syntax
The syntax for the isdigit function in the C Language is:
int isdigit(int c);
Parameters or Arguments
c The value to test whether it is a digit.
Returns
The isdigit function returns a nonzero value if c is a digit and returns zero if c is not a digit.
Required Header
In the C Language, the required header for the isdigit function is:
#include
Applies To
In the C Language, the isdigit function can be used in the following versions:
- ANSI/ISO 9899-1990
isdigit Example
/* Example using isdigit by TechOnTheNet.com */ #include #include int main(int argc, const char * argv[]) < /* Define a temporary variable */ unsigned char test; /* Assign a test decimal digit character to the variable */ test = '7'; /* Test to see if this is a decimal digit character */ if (isdigit(test) != 0) printf("%c is a decimal digit character\n", test); else printf("%c is not a decimal digit character\n", test); /* Assign a non-digit character to the variable */ test = 'T'; /* Test to see if this is a decimal digit character */ if (isdigit(test) != 0) printf("%c is a decimal digit character\n", test); else printf("%c is not a decimal digit character\n", test); return 0; >
When compiled and run, this application will output:
7 is a decimal digit character T is not a decimal digit character
Similar Functions
Other C functions that are similar to the isdigit function:
See Also
Other C functions that are noteworthy when dealing with the isdigit function:
При подготовке материала использовались источники:
https://calmsen.ru/funkcziya-isdigit/
https://www.techonthenet.com/c_language/standard_library_functions/ctype_h/isdigit.php