String To Integer Atoi
To solve this coding challenge, we need to write a function that converts a string into a 32-bit signed integer. This is akin to the atoi function in C/C++. This challenge involves several distinct steps as outlined below.
Explanation
- Whitespace Handling :
- Ignore any leading spaces or tabs. These are considered insignificant.
- Sign Determination :
- Check if the next character (after leading whitespace) is either '-' or '+'. This determines whether the resulting integer is negative or positive. If neither symbol is present, we assume the number is positive.
- Numeric Conversion :
- Read the sequence of digits and convert them into an integer. Ignore any leading zeros because they do not affect the integer value.
- Non-Digit Characters :
- Stop reading the string once a non-digit character is found or the end of the string is reached. Before this stopping point, the characters are converted to their integer equivalents.
- Range Enforcement :
- Ensure that the resulting integer fits within the 32-bit signed integer range, which is between -2,147,483,648 and 2,147,483,647. If the integer is out of this range, we should clamp it to fit within this range.
- Whitespace Handling :
- Remove leading whitespace characters from the string.
- If the string becomes empty after trimming, the function should return 0 as there are no numeric characters to process.
- Sign Determination :
-
If the first character of the trimmed string is '-', set the
sign
-
If the first character is '+', simply remove this character without changing the
sign
- Numeric Conversion :
-
Initialize
integerResult
-
Iterate through each character of the remaining string. If the character is a digit, convert it to its numeric value and update
integerResult
integerResult = integerResult * 10 + digitValue
- If a non-digit character is encountered, break out of the loop as it indicates the end of the number sequence.
- Range Enforcement :
-
Multiply
integerResult
sign
- Check if this final value falls within the 32-bit signed integer range, which is from -2,147,483,648 to 2,147,483,647.
- If the final value is less than -2,147,483,648, return -2,147,483,648.
- If the final value is greater than 2,147,483,647, return 2,147,483,647.
- Otherwise, return the final value.
Pseudocode with Comments:
# Function to convert the given string to a 32-bit signed integer
function myAtoi(inputString):
# Step 1: trim leading whitespace
inputString = trimLeadingWhitespace(inputString)
# If the string is empty, return 0
if inputString is empty:
return 0
# Initialize the result and the sign
integerResult = 0
sign = 1
# Step 2: check for the sign
if inputString starts with '-':
sign = -1
remove the first character from inputString
else if inputString starts with '+':
remove the first character from inputString
# Step 3: convert the numeric part
for each character in inputString:
if character is not a digit:
break
# Update the result by shifting left (multiplying by 10) and adding the new digit
integerResult = integerResult * 10 + convertCharacterToDigit(character)
# Apply the sign to the result
integerResult = sign * integerResult
# Step 4: enforce the 32-bit signed integer range
if integerResult < -2147483648:
return -2147483648
else if integerResult > 2147483647:
return 2147483647
# Return the final result
return integerResult