String to INTEGER (atoi)

Source: Internet
Author: User

Implement atoi to convert a string to an integer.

Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

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

Spoilers alert... click to show requirements for atoi.

Requirements for atoi:

The function first discards as your whitespace characters as necessary until the first non-whitespace character is found. then, starting from this character, takes an optional initial plus or minus sign followed by as your numerical digits as possible, and interprets them as a numerical value.

The string can contain in additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in STR is not a valid integral number, or if no such sequence exists because either STR is empty or it contains only whitespace characters, no conversion is saved med.

If no valid conversion cocould be specified med, a zero value is returned. If the correct value is out of the range of representable values, int_max (2147483647) or int_min (-2147483648) is returned.

 

It is best to consider various input situations before writing code.

Input:

Empty string

125

12.36

546a

#4655

45656

-0001

2147483647

-2147483648

9999999999999999

-99999999999999

1 class solution {2 public: 3 int atoi (const char * Str) {4 If (! Str) return 0; 5 while (* STR & * STR = '') + + STR; // find the first non-space character 6 bool minus = false; 7 if (* STR = '-') {// judge whether it is negative 8 Minus = true; 9 + + STR; 10} 11 else if (* STR = '+') + + STR; // or '+ '12 long ans = 0; // to prevent overflow, use long long13 while (* STR & isdigit (* Str )) {// when a non-digit or Terminator is reached, 14 ans = ans * 10 + (* str-'0'); 15 + + STR; 16} 17 ans = minus? -Ans: ans; // restore the true value 18 if (ANS> int_max) return int_max; // handle overflow 19 else if (ANS <int_min) return int_min; // handle off-flow 20 else return ans; 21} 22 };

 

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.