Convert String to Integer

Source: Internet
Author: User

Title: Enter a String representing an integer to convert the string to an integer and output.

For example, the input string "345", the output integer 345.

Analysis: The problem looks, it is relatively simple, each scan to a character, we put the previously obtained number multiplied by 10 plus the current character represented by the number. This idea is not difficult to achieve with loops. But there are a lot of traps behind it, as Zhedahht said, there are several points you need to note:

1. Since integers may not only contain numbers, it may also start with ' + ' or '-' to denote the positive or negative of integers. If the first character is ' + ', then no action is required, and if the first character is '-' it indicates that the integer is a negative number, and at the end we have to turn the resulting value into a negative number.

2, if using a pointer, before using the pointer, the first thing we should do is to determine whether the pointer is empty. If you try to access a null pointer, it will inevitably cause the program to crash (this 2nd program in the following does not need to be noticed because pointers are not used).

3. The input string may contain characters that are not numeric. Whenever these illegal characters are encountered, there is no need to continue the conversion.

4, overflow problem. Since the input number is entered as a string, it is possible to enter a large numeric conversion that will overflow beyond the largest integer that can be represented.

Convert String to 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.