String to Integer (atoi)
The atoi function, short for "ASCII to Integer," is a fundamental utility in programming. It's used to convert a string representation of a number into its integer equivalent. This is crucial when parsing user input, reading configuration files, or processing data from external sources, where numbers are often represented as strings.
Problem Description
Your task is to implement a function that converts a given string to a 32-bit signed integer, mimicking the behavior of the standard atoi function. The conversion should follow specific rules to handle various input scenarios, including leading/trailing whitespace, signs, non-numeric characters, and overflow conditions.
The function should:
- Ignore leading whitespace: Skip any whitespace characters at the beginning of the string.
- Handle signs: Detect an optional leading sign ('+' or '-'). If no sign is present, assume positive.
- Read digits: Read subsequent characters as long as they are digits ('0'-'9'). Stop reading when a non-digit character is encountered or the end of the string is reached.
- Convert to integer: Convert the sequence of digits into an integer.
- Handle overflow/underflow: If the integer value exceeds the range of a 32-bit signed integer ([-2^31, 2^31 - 1]), clamp the value to the respective boundary.
- Return the result: Return the final integer value. If no valid conversion can be performed (e.g., the string contains only whitespace or starts with non-digit characters after optional whitespace and sign), return 0.
Examples
Example 1:
Input: "42"
Output: 42
Explanation: The string "42" is a simple valid integer.
Example 2:
Input: " -42"
Output: -42
Explanation: Leading whitespace is ignored, and the '-' sign is processed correctly.
Example 3:
Input: "4193 with words"
Output: 4193
Explanation: The digits "4193" are read until a non-digit character (' ') is encountered.
Example 4:
Input: "words and 987"
Output: 0
Explanation: The string starts with non-digit characters ("words") after potential whitespace, so no valid conversion can occur.
Example 5:
Input: "-91283472332"
Output: -2147483648 (which is INT_MIN for a 32-bit signed integer)
Explanation: The number is too large (or too small) to fit in a 32-bit signed integer. It should be clamped to the minimum possible value.
Example 6:
Input: "2147483647"
Output: 2147483647 (which is INT_MAX for a 32-bit signed integer)
Explanation: The number is exactly the maximum value for a 32-bit signed integer.
Example 7:
Input: "+1"
Output: 1
Explanation: A positive sign is handled correctly.
Constraints
- The input
stris a string. - The length of
strcan be between 0 and 200 characters, inclusive. strconsists of English letters (uppercase and lowercase), digits ('0'-'9'), ' ', '+', '-', and '.'.
Notes
- Be mindful of integer overflow. The standard 32-bit signed integer range is from -2<sup>31</sup> to 2<sup>31</sup> - 1.
- Consider using a loop to iterate through the string character by character.
- You might need temporary variables to store the sign and the accumulating integer value.
- Think about how to detect and handle the overflow condition before it actually happens during the conversion.