[Question 100] string to int

Source: Internet
Author: User
# Include <iostream> # include <limits> using namespace STD; Enum status {valid = 0, invalid}; int g_status = valid; int strtoint (const char * Str) {g_status = invalid; long num = 0; If (STR! = NULL) {const char * digit = STR; // is the first letter a plus or minus sign? Bool minus = false; If (* digit = '+') {digit ++;} else if (* digit = '-') {digit ++; minus = true;} while (* digit! = '\ 0') {If (* digit> = '0' & * digit <= '9 ') {num = num * 10 + (* digit-'0'); If (Num> STD: numeric_limits <int >:: max () {num = 0; break;} digit ++;} else {num = 0; break ;}} if (* digit = '\ 0') {g_status = valid; If (minus) {num =-num ;}}return static_cast <int> (Num) ;}void main () {const char * s = "-1234 "; int A = strtoint (s); cout <A <Endl;} // you need to think about the following points: // 1 // invalid input. The pointer is null. // 2 // large number overflow // 3 // What is the difference between NULL pointer and normal 0?

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.