Problem 329
String to Integer
Medium · 20 pts · String, Simulation
Problem
Convert a line of text to a 32-bit signed integer, the way the C function atoi does. Follow these steps exactly. 1) Skip any leading spaces. 2) Read an optional '+' or '-' sign. 3) Read the digits that follow, stopping at the first character that is not a digit (or at the end of the line). 4) If no digits were read, the result is 0. 5) If the value is outside the range [-2147483648, 2147483647], clamp it to the nearest end of that range.
Input
A single line of text (it may contain spaces, letters, digits and symbols).
Output
Print the resulting integer.
Constraints
0 <= length of the line <= 200
Example input
-42 apples
Example output
-42
Explanation
For ' -42 apples' the leading spaces are skipped, the sign '-' is read, then the digits 42; reading stops at the space. The result is -42.