Languages
[Edit]
EN

Java - atoi implementation ASCII to int - custom convert String to int

8 points
Created by:
Suzannah-Estrada
620

1. What ATOI shortcut stands for?

ATOI stands for ASCII to integer.

2. Simple java atoi implementation

Note: this implementation doesn't handle corner cases.
Look at second ATOI implementation (corner cases handled).

static int atoi(String str) {
    int result = 0;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (c >= '0' && c <= '9') {
            result = result * 10 + c - '0';
        }
    }

    if (str.charAt(0) == '-') {
        result *= -1;
    }
    return result;
}

public static void main(String[] args) {
    System.out.println(atoi("123")); // 123
    System.out.println(atoi("+123")); // 123
    System.out.println(atoi("-123")); // -123
    System.out.println(atoi("+0")); // 0
    System.out.println(atoi("10")); // 10
    System.out.println(atoi("-9")); // -9
}

3. Java atoi implementation with corner cases

// Runtime: 2 ms, faster than 59.99% of 
// Java online submissions for String to Integer (atoi).
public static int myAtoi(String str) {
    if (str == null) {
        return 0;
    }
    // remove leading and trailing whitespace
    str = str.trim();
    if (str.isEmpty()) {
        return 0;
    }

    char flag = '+';
    // check negative or positive
    int idx = 0;
    if (str.charAt(0) == '-') {
        flag = '-';
        idx++;
    } else if (str.charAt(0) == '+') {
        idx++;
    }

    double result = 0;
    // main logic
    while (str.length() > idx && str.charAt(idx) >= '0' && str.charAt(idx) <= '9') {
        result = result * 10 + (str.charAt(idx) - '0');
        idx++;
    }

    if (flag == '-') {
        result = -result;
    }

    // check for overflow
    if (result > Integer.MAX_VALUE) {
        return Integer.MAX_VALUE;
    }
    if (result < Integer.MIN_VALUE) {
        return Integer.MIN_VALUE;
    }
    return (int) result; // cast double to int
}

Class with main method + output examples:

public static void main(String[] args) {
    
    // simple println
    System.out.println(myAtoi("123")); // 123
    System.out.println(myAtoi("+123")); // 123
    System.out.println(myAtoi("-123")); // -123
    System.out.println(myAtoi("+0")); // 0
    System.out.println(myAtoi("10")); // 10
    System.out.println(myAtoi("-9")); // -9
    System.out.println(myAtoi("" + Long.MAX_VALUE)); // 2147483647
    System.out.println(myAtoi("" + Long.MIN_VALUE)); // -2147483648
    System.out.println(myAtoi("2147483646")); //
    System.out.println(myAtoi("7193 test test2")); // 7193
    System.out.println(myAtoi("test abc 432")); // 0
    System.out.println(myAtoi(" ")); // 0
    System.out.println(myAtoi(null)); // 0
}

References

  1. leetcode - String to Integer (atoi)
  2. ASCII table wiki
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join