[Leetcode]-string to Integer (atoi)

Source: Internet
Author: User

Mplement atoi to convert a string to an integer.

Hint:carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.

Notes:it is intended-problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C + + function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your cod E definition.

Title: String to Integer
Note: There are a lot of details that need to be considered.
1, int overflow inference
2, sign, details see the main function
3, the case contains a space
Results: 4ms

#include <stdlib.h>#include <stdio.h>#include <math.h>#include <limits.h>intMyatoi (char* str) {//"NULL"  "000234"  "+23" " -34" "+-34"  "010"    if(NULL = = str)return 0;intsum =0.0;intf =0;intDIV = int_max/Ten; while(' + '!=*str)        {if(*str==' + ') {str++;if(*str<' 0 '||*str>' 9 ')return 0;Elsef =0; }if(*str=='-') {str++;if(*str<' 0 '||*str>' 9 ')return 0;Elsef =1; }if(*str>=' 0 '&&*str<=' 9 ')        {if(Sum > DIV && f = =0)returnInt_max;if(Sum > DIV && f = =1)return-int_max-1; sum = SUM *10.0;if(Int_max-*str+' 0 '< sum && F = =0)returnInt_max;if((Int_max-(*str-' 0 ') < sum) && (f = =1))return-int_max-1; sum = sum +*str-' 0 '; str++;if(*str==' + ') Break;Else if(*str<' 0 '||*str>' 9 ') Break; }Else{if(*str>=' A '&&*str<=' Z ') Break;        str++; }    }if(f) sum =-sum;returnsum;}intMain () {Char*str="0034";intR = Myatoi (str);printf("Myatoi str0 is: %d\ n", R); Char*STR1="XX";intR1 = Myatoi (str1);printf("Myatoi str1 is: %d\ n", R1); Char*STR2="+23";intr2 = Myatoi (str2);printf("Myatoi str2 is: %d\ n", r2); Char*STR3=" -2345";intR3 = Myatoi (STR3);printf("Myatoi STR3 is: %d\ n", R3); Char*STR4="+-23";intR4 = Myatoi (STR4);printf("Myatoi STR4 is: %d\ n", R4);//Expected0Char*STR5="010";intR5 = Myatoi (STR5);printf("Myatoi STR5 is: %d\ n", R5); Char*STR6="+0104";intR6 = Myatoi (STR6);printf("Myatoi STR6 is: %d\ n", R6); Char*STR7=" -0187";intR7 = Myatoi (STR7);printf("Myatoi STR7 is: %d\ n", R7);//Expected-187Char*str8=" -018a567";intR8 = Myatoi (STR8);printf("Myatoi str8 is: %d\ n", R8);//Expected- -Char*STR9="2147483648";intR9 = Myatoi (STR9);printf("Myatoi STR9 is: %d\ n", R9);//Expected2147483647Just take the maximum char because it's out of bounds*str10=" -2147483649";intR10= Myatoi (str10);printf("Myatoi Str10 is: %d\ n", R10);//Expected-2147483648Char*str11="-204";intR11 = Myatoi (STR11);printf("Myatoi Str11 is: %d\ n", R11);//Expected0Char*str12="b3424242";intR12 = Myatoi (str12);printf("Myatoi str12 is: %d\ n", R12);//Expected0       while(0);}

[leetcode]-string to Integer (atoi)

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.