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