Open In App

String to Integer - Write your own atoi()

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string s, the task is to convert it into integer format without utilizing any built-in functions. Refer the below steps to know about atoi() function.

Examples:

Input: s = "-123"
Output: -123

Input: s = " -"
Output: 0
Explanation: No digits are present, therefore 0.

Input: s = " 1231231231311133"
Output: 2147483647
Explanation: The converted number is greater than 231 - 1, therefore print 231 - 1 = 2147483647.

Input: s = "-999999999999"
Output: -2147483648
Explanation: The converted number is smaller than -231, therefore print -231 = -2147483648.

Input: s = " -0012gfg4"
Output: -12
Explanation: Nothing is read after -12 as a non-digit character 'g' was encountered.

The basic idea is to follow the atoi() algorithm in order and covering all the edge cases:

  • Skip the leading whitespaces by iterating from the first character.
  • Now, check for at most one sign character ('+' or '-') and maintain a sign variable to keep track of the sign of the number.
  • Finally, read all the digits and construct the number until the first non-digit character is encountered or end of the input string is reached.
  • While constructing the number, if the number becomes greater than 231 - 1, print 231 - 1. Similarly, if the number becomes less than -231, print -231.

How to check if the number is greater than 231 - 1 or smaller than -231 ?
The naive way is to use a data type which has size greater than 32 bits like long, BigInteger to store the number. However, we can also use 32-bit integer by appending the digits one-by-one and for each digit, check if appending current digit to the number will make it underflow (< -231) or overflow(> 231- 1). While appending a digit to the current number, we can have 3 cases:
Case 1: current number < (231 - 1)/10 or current number > -231/10: Simply append the digit to the current number as it won't cause overflow/underflow.
Case 2: current number > (231 - 1)/10 or current number < -231/10: Return (231 - 1) in case of overflow and -231 in case of underflow.
Case 3: current number = (231 - 1)/10 or current number = -231/10: In this case, if current number = (231 - 1)/10, then only 0-7 digits can be appended and if current number = -231/10, then only 0-8 digits can be appended.

In the below implementation, we are constructing the absolute value of the number, so we can simply compare with (231 - 1)/10 and avoid comparing with -231/10.

C++
#include <bits/stdc++.h>
using namespace std;

int myAtoi(char* s) {
    int sign = 1, res = 0, idx = 0;

    // Ignore leading whitespaces
    while (s[idx] == ' ') {
        idx++;
    }

    // Store the sign of number
    if (s[idx] == '-' || s[idx] == '+') {
      	if(s[idx++] == '-')
          sign = -1;
    }

    // Construct the number digit by digit
    while (s[idx] >= '0' && s[idx] <= '9') {
        
        // handling overflow/underflow test case
        if (res > INT_MAX / 10 || (res == INT_MAX / 10 && s[idx] - '0' > 7)) {
            return sign == 1 ? INT_MAX : INT_MIN;
        }
      
        // Append current digit to the result
        res = 10 * res + (s[idx++] - '0');
    }
    return res * sign;
}

int main() {
    char s[] = " -0012g4";
    //  -0012g4
    cout << myAtoi(s);
    return 0;
}
C
#include <stdio.h>
#include <limits.h>
int myAtoi(char* s) {
    int sign = 1, res = 0, idx = 0;
    // Ignore leading whitespaces
    while (s[idx] == ' ') {
        idx++;
    }
    // Store the sign of number
    if (s[idx] == '-' || s[idx] == '+') {
        if (s[idx++] == '-') {
            sign = -1;
        }
    }
    // Construct the number digit by digit
    while (s[idx] >= '0' && s[idx] <= '9') {
        // Handling overflow/underflow test case
        if (res > INT_MAX / 10 || (res == INT_MAX / 10 && s[idx] - '0' > 7)) {
            return sign == 1 ? INT_MAX : INT_MIN;
        }
        // Append current digit to the result
        res = 10 * res + (s[idx++] - '0');
    }
    return res * sign;
}

int main() {
    char s[] = " -0012g4";
    printf("%d", myAtoi(s));
    return 0;
}
Java
class GfG {
    static int myAtoi(String s) {
        int sign = 1, res = 0, idx = 0;
// Ignore leading whitespaces
        while (idx < s.length() && s.charAt(idx) == ' ') {
            idx++;
        }
// Store the sign of number
        if (idx < s.length() && (s.charAt(idx) == '-' 
                                 || s.charAt(idx) == '+')) {
            if (s.charAt(idx++) == '-') {
                sign = -1;
            }
        }
// Construct the number digit by digit
        while (idx < s.length() && s.charAt(idx) >= '0' 
               						&& s.charAt(idx) <= '9') {
            
            // Handling overflow/underflow test case
            if (res > Integer.MAX_VALUE / 10 || 
                   (res == Integer.MAX_VALUE / 10 && s.charAt(idx) - '0' > 7)) {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
          
            // Append current digit to the result
            res = 10 * res + (s.charAt(idx++) - '0');
        }
        return res * sign;
    }

    public static void main(String[] args) {
        String s = "  -0012g4";
        System.out.println(myAtoi(s));
    }
}
Python
def myAtoi(s: str) -> int:
    sign = 1
    res = 0
    idx = 0

    # Ignore leading whitespaces
    while idx < len(s) and s[idx] == ' ':
        idx += 1

    # Store the sign of number
    if idx < len(s) and (s[idx] == '-' or s[idx] == '+'):
        if s[idx] == '-':
            sign = -1
        idx += 1

    # Construct the number digit by digit
    while idx < len(s) and '0' <= s[idx] <= '9':

        # Append current digit to the result
        res = 10 * res + (ord(s[idx]) - ord('0'))

        # Handling overflow/underflow test case
        if res > (2**31 - 1):
            return sign * (2**31 - 1) if sign == 1 else -2**31

        idx += 1

    return res * sign

s = " -0012g4"
print(myAtoi(s))
C#
using System;

class GfG {
    public static int MyAtoi(string s) {
        int sign = 1, res = 0, idx = 0;
        // Ignore leading whitespaces
        while (idx < s.Length && s[idx] == ' ') {
            idx++;
        }
        // Store the sign of number
        if (idx < s.Length && (s[idx] == '-' || s[idx] == '+')) {
            if (s[idx] == '-')
                sign = -1;
            idx++;
        }
        // Construct the number digit by digit
        while (idx < s.Length && s[idx] >= '0' && s[idx] <= '9') {
            // Handling overflow/underflow test case
            if (res > Int32.MaxValue / 10 || 
                	(res == Int32.MaxValue / 10 && s[idx] - '0' > 7)) {
                return sign == 1 ? Int32.MaxValue : Int32.MinValue;
            }
            // Append current digit to the result
            res = 10 * res + (s[idx] - '0');
            idx++;
        }
        return res * sign;
    }

    static void Main() {
        string s = " -0012g4";
        Console.WriteLine(MyAtoi(s));
    }
}
JavaScript
function myAtoi(s) {
    let sign = 1, res = 0, idx = 0;
 // Ignore leading whitespaces
    while (idx < s.length && s[idx] === ' ') {
        idx++;
    }
    // Store the sign of number
    if (idx < s.length && (s[idx] === '-' || s[idx] === '+')) {
        if (s[idx] === '-') {
            sign = -1;
        }
        idx++;
    }
    // Construct the number digit by digit
    while (idx < s.length && s[idx] >= '0' && s[idx] <= '9') {
        
        // Handling overflow/underflow test case
        if ((res > Math.pow(2, 31) / 10) || ((res === Math.floor(Math.pow(2, 31) / 10)) && (s[idx] - '0' > 7))) {
            
            return sign === 1 ? (Math.pow(2, 31) - 1) : -Math.pow(2, 31);
        }
        // Append current digit to the result
        res = 10 * res + (s[idx] - '0');
        idx++;
    }
    return (res * sign == -0 ? 0 : res * sign);
}

const s = " -0012g4";
console.log(myAtoi(s));

Output
-12

Working:


  • Time Complexity: O(n), Only one traversal of the string is needed.
  • Auxiliary Space: O(1), As no extra space is required.

Related Articles:

Write your won atof() that takes a string (which represents a floating point value) as an argument and returns its value as double.



Next Article

Similar Reads