Convert a string to a 32-bit signed integer, following atoi parsing rules for whitespace, sign, digits, and overflow clamping.
atoi
gfgGiven a string s, interpret it as a 32-bit signed integer using rules similar to the classic atoi function.
Parse the string from left to right and build the integer result under these rules:
+ or -.0.Return the final integer value.
s.The string may contain leading spaces, an optional sign, digits, and other trailing characters.
[$-2^{31}$, $2^{31}-1$].0.Example 1
Input
s = "42"
Output
42
Explanation
The string starts with digits, so the parsed integer is 42.
Example 2
Input
s = " -42"
Output
-42
Explanation
Leading spaces are ignored, then the negative sign is applied to the parsed digits.
Example 3
Input
s = "4193 with words"
Output
4193
Explanation
Parsing stops when a non-digit character is encountered after reading digits.
Example 4
Input
s = "words and 987"
Output
0
Explanation
No valid numeric prefix starts at the beginning after skipping leading whitespace.
Example 5
Input
s = "-91283472332"
Output
-2147483648
Explanation
The value is smaller than the 32-bit signed minimum, so it is clamped.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.