Last Updated: 16 Dec, 2020

Implement Atoi Function

Hard
Asked in companies
AmazonAdobeMorgan Stanley

Problem statement

You are given a string ‘s’ of length 'n'.


Implement the atoi(string s) function, which converts a given string into a 32-bit signed integer, following similar principles as the atoi function in C/C++.


Here's the step-by-step algorithm for myAtoi(string s):


1. Discard any leading whitespaces.


2. If the next character (if not at the end of the string) is '-' or '+', consider it to determine the sign of the result. If neither is present, assume the result is positive.


3. Read and accumulate digits until a non-digit character is encountered or the end of the input is reached.


4. Convert the collected digits into an integer (e.g., "123" becomes 123, "0032" becomes 32). If no digits were read, the integer is 0. Adjust the sign as needed (as determined in step 2).


5. If the integer falls outside the range of a 32-bit signed integer [-2^31, 2^31 - 1], constrain it to stay within the range. For instance, if the integer is less than -2^31, set it to -2^31; if it's greater than 2^31 - 1, set it to 2^31 - 1.


6. Return the resulting integer.


Note :
1. Only the space character ' ' is treated as a whitespace.

2. All characters other than leading whitespace or digits are considered.


Example:
Input: 45rohit12

Output: 45

Explanation: 
Leading Whitespace: Leading whitespace characters (" ") are ignored.

Numeric Portion: The numeric portion starts with the digit "4".

Reading Numeric Characters: The algorithm reads "4" and "5" to form "45".

End of Numeric Portion: The numeric portion ends at "r", non-numeric characters are skipped.

Parsing Result: "45" is parsed into the integer value 45, the final result.
Input format :
The only line of the test case contains the string ‘s’.
Output format :
The output contains the integer formed by the given string.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.


Approaches

01 Approach

Approach:
The approach involves parsing a string to convert it into a 32-bit signed integer. The algorithm starts by skipping leading whitespaces. It detects positive or negative signs and accumulates the numeric portion of the string. By converting characters to integers, it forms the integer value. The approach handles cases of negative signs, sign clashes, and integer range limits. The result is returned as the final output. 
 

Algorithm :

FUNCTION createAtoi(s: string) -> 

  • Initialise i = 0
  • n = length of s
  • WHILE s[i] is whitespace
    • i++
  • Initialise positive = 0
  • Initialise negative = 0
  • IF s[i] is '+'
    • positive++
    • i++
  • ELSE IF s[i] is '-'
    • negative++
    • i++
  • Initialise ans = 0
  • WHILE i < n AND s[i] is a digit (0-9)
    • ans = ans * 10 + (s[i] - '0')
    • i++
  • IF negative > 0
  • ans = -1*ans
  • IF positive > 0 AND negative > 0
    • RETURN 0
  • IF ans > INT_MAX
    • ans = INT_MAX
  • ELSE IF ans < INT_MIN
    • ans = INT_MIN
  • RETURN ans