C language determines whether a string is a number

Source: Internet
Author: User

C language determines whether a string is a number

 

It sounds simple to determine whether a string is a number, and the implementation is still a bit difficult. I recently wrote one, as follows:

 

#define IS_BLANK(c) ((c) == ' ' || (c) == '\t')#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')#define IS_ALPHA(c) ( ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') )#define IS_HEX_DIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))/* Whether string s is a number.    Returns 0 for non-number, 1 for integer, 2 for hex-integer, 3 for float */int is_number(char * s){int base = 10;char *ptr;int type = 0;if (s==NULL) return 0;ptr = s;/* skip blank */while (IS_BLANK(*ptr)) {ptr++;}/* skip sign */if (*ptr == '-' || *ptr == '+') {ptr++;}/* first char should be digit or dot*/if (IS_DIGIT(*ptr) || ptr[0]=='.') {if (ptr[0]!='.') {/* handle hex numbers */if (ptr[0] == '0' && ptr[1] && (ptr[1] == 'x' || ptr[1] == 'X')) {type = 2;base = 16;ptr += 2;}/* Skip any leading 0s */while (*ptr == '0') {ptr++;}/* Skip digit */while (IS_DIGIT(*ptr) || (base == 16 && IS_HEX_DIGIT(*ptr))) {ptr++;}}/* Handle dot */if (base == 10 && *ptr && ptr[0]=='.') {type = 3;ptr++;}/* Skip digit */while (type==3 && base == 10 && IS_DIGIT(*ptr)) {ptr++;}/* if end with 0, it is number */if (*ptr==0) return (type>0) ? type : 1;elsetype = 0;}return type;}


 

 

The is_number (char *) function determines whether the string is a number. If not, 0 is returned. If it is an integer, return 1. If it is a hexadecimal integer, 2 is returned. If it is a decimal number, 3 is returned.

 

Compile a test program:

 

 

#include 
 
  #include int main(int argc, char**argv){assert( is_number(NULL)  ==0 );assert( is_number("")    ==0 );assert( is_number("9a")  ==0 );assert( is_number("908") ==1 );assert( is_number("-908") ==1 );assert( is_number("+09")  ==1 );assert( is_number("-+9")  ==0 );assert( is_number("  007")  ==1 );assert( is_number("0x9a8F") ==2 );assert( is_number("-0xAB")  ==2 );assert( is_number("-9.380") ==3 );assert( is_number("-0xFF.3") ==0 );printf("test OK\n");}
 


 

Run, "test OK"

 

 

Related Article

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.