As the question asks. How to determine whether a string is a full digit in C language.
Post a piece of code first
/**
* @brief Function isdigitstr () to determine whether an incoming string is full number
* @param [in] char *str string
* @ retval 1: Full string, 0: Non-full string
* @pre
* @post
/static int isdigitstr (char *str)
{ Return
(strspn (str, "0123456789") ==strlen (str));
You can use the combination of STRSPN and strlen to determine whether a string is full digits. STRSPN returns how many characters in Str are the same as the parameter "0123456789", while the full number is exactly the same as the strlen,
So this combination can determine whether a string is a full digit.
STRSPN Definition: Table header file
To define a function:
| 1 |
size_t strspn (const char *s, const char * accept); |
The function Description strspn () computes contiguous characters from the beginning of the argument s string, which are all characters in the accept string. Simply put, if STRSPN () returns a value of N, then the beginning of the string s begins with n characters that belong to the character within the string accept. Returns a value that returns the number of characters in the string accept containing the string s at the beginning