To convert a string into an integer (like the atoi
function), we need to carefully interpret the string one character at a time. It's not just about parsing digits — we must also think about signs, spaces, invalid characters, and potential overflows.
Let’s break it down for a beginner:
1. Ignore leading whitespaces: The first thing we do is skip any space characters at the beginning of the string. These should not affect the result.
2. Check for optional '+' or '-' sign: After trimming spaces, if the first character is '+' or '-', we store this to determine the final sign of our number.
3. Convert following digits: Next, we move character by character and as long as we see digits (0-9), we build the number by multiplying the existing result by 10 and adding the new digit.
4. Stop on invalid character: If we hit a character that's not a digit (e.g., letter, punctuation, space), we stop immediately. Only the valid prefix of the string is considered.
5️⃣ Handle overflow: While building the number, if at any point it goes beyond the limits of a 32-bit signed integer, we stop and clamp the result to:
2147483647
if the number is too large (positive overflow)
-2147483648
if the number is too small (negative overflow)
6️⃣ Empty or invalid string: If after removing spaces and optional sign we find nothing to convert, or the string starts with something invalid, we simply return 0
.
Example thoughts:
- "42" → clean numeric string, converts to 42.
- " -42" → spaces skipped, '-' sign handled, digits converted → -42.
- "words and 987" → starts with non-digit → invalid → return 0.
- "91283472332" → valid number but too large → clamp to 2147483647.
- "" or " " → empty after trimming → return 0.
This approach ensures correctness and protects against errors, making it suitable for real-world use cases like parsing user input.