Implement the Strtoint function yourself

Source: Internet
Author: User
Tags strlen
int Strtoint (const char*pstr,int defaultval) {//NULL pointer access if (pstr==0) return defaultval;
    int result=1,tem=0;
    Number of digits int bitnum=0;
    symbol int sign=1;
    const char *PTEM=PSTR;
        Detects whether the first character is-or the number if (*ptem== '-') {sign=-1;
                Whether it is at the end or in the middle there is ' if ' ((*++ptem)!=0) {if (*ptem> ' 0 ' &&*ptem<= ' 9 ') {
                -1 *=n result*= (*ptem-' 0 ');
            Calculate the number of bits bitnum= (strlen (PSTR)-2);
            } else {return defaultval;
        }} else {return defaultval;
        }} else if (*ptem> ' 0 ' &&*ptem<= ' 9 ') {result= (*ptem-' 0 ');
    Calculate the number of bits bitnum= (strlen (PSTR)-1);
    } else {return defaultval;
    } ptem++; Detects that the remainder of the string has no illegal ' *ptem!=0 ' truncation while (a) {///illegal if (*ptem< ' 0 ' | | *ptem> ' 9') return defaultval;
        Calculation results Tem=tem*10+n tem=tem*10+ (*ptem-' 0 ');
    ptem++;
}//combination first and subsequent digits return (Tem+result*pow (10,bitnum)) *sign; }

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.