How does C language check whether a string is full number?

Source: Internet
Author: User

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

1 #include <string.h>
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.