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.