🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
Can you solve this real interview question? String to Integer (atoi) - Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: 1. Whitespace: Ignore any leading ...
🌐
AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
The clever part of this solution is the preemptive overflow check. By comparing against (2**31 - 1) // 10 and checking the last digit separately when equal, we avoid actual integer overflow in our calculation. This ensures the algorithm works correctly even in languages with fixed integer sizes. The time complexity is O(n) where n is the length of the string, as we traverse it at most once.
Discussions

After 3 hours of pain and misery, my solution to “String to Integer” on LeetCode
Your approach to taking screenshots is deeply disturbing. More on reddit.com
🌐 r/programminghorror
169
2245
January 4, 2024
atoi the function used to convert a string to an integer...but...
I don't understand what you're asking at all. This function can only be used on a string that can be parsed as a series of decimal digits (optionally with a sign), not any arbitrary string. If you try to convert a string that's not in that format, then it fails and returns zero. That's also why this function is deprecated and should not be used, because you can't tell the difference between a successful conversion of the string representation of zero and the failure case of an invalid string. More on reddit.com
🌐 r/C_Programming
20
1
July 17, 2016
Atoi.
The atoi function is for converting a string (that is, char*) to an int. A char is not a string. The good news is that char is already a numeric type. You can just write int num = argv[1][k]; print("success: %d\n", num); Even if you do need to convert a string to an int, you shouldn't use atoi, because it lacks error handling. You can pass in "flurbl" and it will still return an int value. Instead you should use strtol or sscanf. In the former case you need to set errno=0 first, in order to distinguish overflow from a correct conversion of INT_MAX or INT_MIN. More on reddit.com
🌐 r/C_Programming
8
0
April 17, 2019
Atoi
atoi converts a string to a number. It will not work with individual characters, only with a null-terminated string. You seem to be trying to convert individual characters of argv[1] to integers, rather than trying to convert argv[1] itself. Note that you should only try to convert argv[1] after you have checked all characters of argv[1] to make sure that they are valid digits. More on reddit.com
🌐 r/cs50
11
4
April 17, 2019
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-your-own-atoi
String to Integer - Write your own atoi() - GeeksforGeeks
Write your own atoi() that takes a string (which represents a floating point value) as an argument and returns its value as double.
Published: April 14, 2026
🌐
Medium
medium.com › @harycane › string-to-integer-atoi-1e75325c5fd7
String to Integer (atoi). Implement atoi which converts a string… | by Hary Krishnan | Medium
August 1, 2018 - Prior to taking care of the numeric case, care has to be taken care in order to ensure that the current character is not out of the asci range of ‘0’ or ‘9’. If it so, then according to the given rule, we break from the string iteration to return the result, subject to certain conditions. 5 If isNegative is true, then we toggle the result to point to the negative version of its value. 6 If result which is of type double, is less than Integer.MIN_VALUE, then we return Integer.MIN_VALUE.
🌐
WalkCCC
walkccc.me › LeetCode › problems › 8
8. String to Integer (atoi) - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0000-0099 › 0008.String to Integer (atoi) › README_EN.md
leetcode/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md at main · doocs/leetcode · GitHub
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. Rounding: If the integer is out of the 32-bit signed integer ...
Author: doocs
🌐
Red Quark
redquark.org › leetcode › 0008-string-to-integer-atoi
LeetCode #8 - String To Integer (atoi) | Red Quark
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
🌐
Takeuforward
takeuforward.org › practice › dsa › string-to-integer-atoi
String to Integer (atoi) - DSA Practice | takeUforward
Implement the function myAtoi(s) which converts the given string s to a 32-bit signed integer (similar to the C/C++ atoi function).
Find elsewhere
🌐
PREP INSTA
prepinsta.com › home › leetcode top 100 liked questions with solution › string to integer (atoi) leetcode solution
String to Integer (atoi) Leetcode Solution
January 9, 2024 - Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer (i.e. “123” -> 123, “0032” -> 32).
🌐
DEV Community
dev.to › jaspreet_singh_86ae1740ac › string-to-integer-atoi-2nki
String to Integer (atoi) - DEV Community
June 30, 2026 - // Trim spaces // Store digits into StringBuilder // Convert using Integer.parseInt() // Handle exceptions for overflow ... Build the answer digit by digit. ... Skip leading spaces. Determine sign. ... Process every digit. ... class Solution { public int myAtoi(String s) { int i = 0; int n = s.length(); while (i < n && s.charAt(i) == ' ') i++; int sign = 1; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { if (s.charAt(i) == '-') sign = -1; i++; } int number = 0; while (i < n && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0'; if (number > (Integer.MAX_VALUE - digit) / 10) { return sign == 1 ?
🌐
DEV Community
dev.to › grantdotdev › leetcode-8-string-to-integer-atoi-medium-33om
LeetCode 8: String to Integer (atoi) - Medium - DEV Community
September 21, 2025 - Check Bounds: As the number is built, ensure it stays within the 32-bit integer limits, clamping to int.MaxValue or int.MinValue if necessary. Apply the Sign: Multiply the result by the sign to get the correct positive or negative value. This approach is clean, avoids heavy string operations, and handles all edge cases efficiently. class Solution { public int MyAtoi(string s) { // Handle null or empty string if (string.IsNullOrEmpty(s)) return 0; int idx = 0, end = s.Length; // Skip leading whitespace while (idx < end && s[idx] == ' ') idx++; if (idx == end) return 0; // Return 0 if only whitespace // Check for sign int sign = 1; // Default to positive if (s[idx] == '+' || s[idx] == '-') { sign = (s[idx] == '-') ?
🌐
SyntaxFlow
syntaxflowarticles.pages.dev › home › data structures and algorithms › string to integer (atoi) – leetcode 8 c++ solution & dry run
String to Integer (atoi) – LeetCode 8 C++ Solution & Dry Run | SyntaxFlow
July 13, 2026 - Extract the integer value of the character: digit = s[index] - '0'. Overflow Check: If result > INT_MAX / 10 OR (result == INT_MAX / 10 && digit > 7): If sign == 1, return INT_MAX.
🌐
PROGIEZ
progiez.com › 8-string-to-integer-atoi-leetcode-solution
8. String To Integer (atoi) LeetCode Solution - Progiez
September 12, 2024 - class Solution { public int myAtoi(String s) { s = s.strip(); if (s.isEmpty()) return 0; final int sign = s.charAt(0) == '-' ? -1 : 1; if (s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1); long num = 0; for (final char c : s.toCharArray()) { if (!Character.isDigit(c)) break; num = num * 10 + (c - '0'); if (sign * num <= Integer.MIN_VALUE) return Integer.MIN_VALUE; if (sign * num >= Integer.MAX_VALUE) return Integer.MAX_VALUE; } return sign * (int) num; } } // code provided by PROGIEZ
🌐
Medium
medium.com › @rutujakbhombe19 › solving-leetcode-until-i-land-a-job-306a30914738
Leetcode 8 : String to Integer (atoi) | by Rutuja Bhombe | Medium
July 17, 2024 - Read each character until a non-digit character is encountered or the end of the string is reached. Convert these characters into a number. ... If the number is outside the range of a 32-bit signed integer, clamp it to the range limits.
🌐
GitHub
github.com › ChenglongChen › LeetCode-3 › blob › master › Python › string-to-integer-atoi.py
LeetCode-3/Python/string-to-integer-atoi.py at master · ChenglongChen/LeetCode-3 · GitHub
:pencil: Python / C++ 11 Solutions of All 468 LeetCode Questions - LeetCode-3/Python/string-to-integer-atoi.py at master · ChenglongChen/LeetCode-3
Author: ChenglongChen
🌐
Theresearchguy
theresearchguy.com › home › string to integer (atoi)
String to Integer (atoi) – theresearchguy.com
April 13, 2026 - public int myAtoi(String s) { int i = 0, n = s.length(), sign = 1; long result = 0; while (i < n && s.charAt(i) == ' ') i++; if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) { sign = s.charAt(i) == '-' ? -1 : 1; i++; } while (i < n && Character.isDigit(s.charAt(i))) { result = result * 10 + (s.charAt(i) - '0'); if (result * sign > Integer.MAX_VALUE) return Integer.MAX_VALUE; if (result * sign < Integer.MIN_VALUE) return Integer.MIN_VALUE; i++; } return (int)(result * sign); }
🌐
GhostInterview
ghostinterview.co › home › leetcode problems › string to integer (atoi)
String to Integer (atoi) LeetCode Problem Guide | GhostInterview
March 8, 2026 - This ensures precise extraction from a string-driven input. While accumulating digits, check if adding the next digit would exceed the 32-bit signed integer range. If overflow is detected, clamp the result to INT_MAX or INT_MIN depending on the sign. This prevents common failure modes in atoi conversions.
🌐
CodingBroz
codingbroz.com › home › leetcode › string to integer (atoi) – leetcode solution
String to Integer (atoi) - Leetcode Solution - CodingBroz
September 20, 2022 - Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32).