Convert a string to an integer

Source: Internet
Author: User

Note the problem:

1) The case that the string is a null pointer.

2) Assume that the input is an empty string ""

3) are all characters outside the ' 0 ' to ' 9 ' illegal? The plus and minus signs are legal inputs.

4) Consider overflow

#include <iostream>using namespace Std;bool g_value=false;int  atoi (char * number) {if (number = = NULL | | *number = = ' + ') {G_value=true;return 0;}    Double count=0;//here is a double type bool Min=false;if (*number== '-') {min=true;number++;} else if (' + ' ==*number) {min=false;number++;} Elsemin=false;while (* (number) = ' \ ") {if ((*number-' 0 ') >=0&& (*number-' 0 ') <=9) {count = count*10 + (* number-' 0 '); if (false==min&&count> 0x7FFFFFFF | | true==min&&count< (signed int) 0x80000000) {cout<< "Overflow" <<endl;count=0;        break;} number++;} else{break;}} if (min) count*=-1;return (int) count;} int main () {char * number= " -11111"; cout<<atoi (number); return 0;}

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvynv5aw5nzmvpodg4oa==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">

Memmove and memcpy differences and implementations: http://www.cnblogs.com/foxinhongyan/articles/1267624.html



Convert a string to an integer

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.