The TransCompilator

Solved LeetCode program · 8 of 1,000

#0008 · String to Integer (atoi)

Medium String chunk-01.md

Original English source

The word is "   -42". The final is one less than the number of characters in word. The position is 0. The value is 0. The sign is 1.
Repeat while position is at most final and character position of word is 32.
Add 1 to position.

If position is at most final and character position of word is 45, set sign to -1, add 1 to position. If position is at most final and character position of word is 43, add 1 to position.
Repeat while position is at most final and character position of word is between "0" and "9".
The digit is the digit value of character position of word. Set value to value times 10 plus digit. Add 1 to position.

If sign is -1, set value to 0 minus value. If value is more than 2147483647, set value to 2147483647. If value is less than -2147483648, set value to -2147483648. Report value.

Open in Playground Browse all 1,000 problems

Expected stdout

-42